driver-SPI-bitmine-A1.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*
  2. * cgminer SPI driver for Bitmine.ch A1 devices
  3. *
  4. * Copyright 2013, 2014 Zefir Kurtisi <zefir.kurtisi@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include <stdlib.h>
  12. #include <assert.h>
  13. #include <fcntl.h>
  14. #include <limits.h>
  15. #include <unistd.h>
  16. #include <stdbool.h>
  17. #include "spi-context.h"
  18. #include "logging.h"
  19. #include "miner.h"
  20. #include "util.h"
  21. #include "A1-common.h"
  22. #include "A1-board-selector.h"
  23. #include "A1-trimpot-mcp4x.h"
  24. /* one global board_selector and spi context is enough */
  25. static struct board_selector *board_selector;
  26. static struct spi_ctx *spi;
  27. /********** work queue */
  28. static bool wq_enqueue(struct work_queue *wq, struct work *work)
  29. {
  30. if (work == NULL)
  31. return false;
  32. struct work_ent *we = malloc(sizeof(*we));
  33. assert(we != NULL);
  34. we->work = work;
  35. INIT_LIST_HEAD(&we->head);
  36. list_add_tail(&we->head, &wq->head);
  37. wq->num_elems++;
  38. return true;
  39. }
  40. static struct work *wq_dequeue(struct work_queue *wq)
  41. {
  42. if (wq == NULL)
  43. return NULL;
  44. if (wq->num_elems == 0)
  45. return NULL;
  46. struct work_ent *we;
  47. we = list_entry(wq->head.next, struct work_ent, head);
  48. struct work *work = we->work;
  49. list_del(&we->head);
  50. free(we);
  51. wq->num_elems--;
  52. return work;
  53. }
  54. /*
  55. * if not cooled sufficiently, communication fails and chip is temporary
  56. * disabled. we let it inactive for 30 seconds to cool down
  57. *
  58. * TODO: to be removed after bring up / test phase
  59. */
  60. #define COOLDOWN_MS (30 * 1000)
  61. /* if after this number of retries a chip is still inaccessible, disable it */
  62. #define DISABLE_CHIP_FAIL_THRESHOLD 3
  63. enum A1_command {
  64. A1_BIST_START = 0x01,
  65. A1_BIST_FIX = 0x03,
  66. A1_RESET = 0x04,
  67. A1_WRITE_JOB = 0x07,
  68. A1_READ_RESULT = 0x08,
  69. A1_WRITE_REG = 0x09,
  70. A1_READ_REG = 0x0a,
  71. A1_READ_REG_RESP = 0x1a,
  72. };
  73. /*
  74. * for now, we have one global config, defaulting values:
  75. * - ref_clk 16MHz / sys_clk 800MHz
  76. * - 2000 kHz SPI clock
  77. */
  78. struct A1_config_options A1_config_options = {
  79. .ref_clk_khz = 16000, .sys_clk_khz = 800000, .spi_clk_khz = 2000,
  80. };
  81. /* override values with --bitmine-a1-options ref:sys:spi: - use 0 for default */
  82. static struct A1_config_options *parsed_config_options;
  83. /********** temporary helper for hexdumping SPI traffic */
  84. static void applog_hexdump(char *prefix, uint8_t *buff, int len, int level)
  85. {
  86. static char line[256];
  87. char *pos = line;
  88. int i;
  89. if (len < 1)
  90. return;
  91. pos += sprintf(pos, "%s: %d bytes:", prefix, len);
  92. for (i = 0; i < len; i++) {
  93. if (i > 0 && (i % 32) == 0) {
  94. applog(LOG_DEBUG, "%s", line);
  95. pos = line;
  96. pos += sprintf(pos, "\t");
  97. }
  98. pos += sprintf(pos, "%.2X ", buff[i]);
  99. }
  100. applog(level, "%s", line);
  101. }
  102. static void hexdump(char *prefix, uint8_t *buff, int len)
  103. {
  104. applog_hexdump(prefix, buff, len, LOG_DEBUG);
  105. }
  106. static void hexdump_error(char *prefix, uint8_t *buff, int len)
  107. {
  108. applog_hexdump(prefix, buff, len, LOG_ERR);
  109. }
  110. static void flush_spi(struct A1_chain *a1)
  111. {
  112. memset(a1->spi_tx, 0, 64);
  113. spi_transfer(a1->spi_ctx, a1->spi_tx, a1->spi_rx, 64);
  114. }
  115. /********** upper layer SPI functions */
  116. static uint8_t *exec_cmd(struct A1_chain *a1,
  117. uint8_t cmd, uint8_t chip_id,
  118. uint8_t *data, uint8_t len,
  119. uint8_t resp_len)
  120. {
  121. int tx_len = 4 + len;
  122. memset(a1->spi_tx, 0, tx_len);
  123. a1->spi_tx[0] = cmd;
  124. a1->spi_tx[1] = chip_id;
  125. if (data != NULL)
  126. memcpy(a1->spi_tx + 2, data, len);
  127. assert(spi_transfer(a1->spi_ctx, a1->spi_tx, a1->spi_rx, tx_len));
  128. hexdump("send: TX", a1->spi_tx, tx_len);
  129. hexdump("send: RX", a1->spi_rx, tx_len);
  130. int poll_len = resp_len;
  131. if (chip_id == 0) {
  132. if (a1->num_chips == 0) {
  133. applog(LOG_INFO, "%d: unknown chips in chain, "
  134. "assuming 8", a1->chain_id);
  135. poll_len += 32;
  136. }
  137. poll_len += 4 * a1->num_chips;
  138. }
  139. else {
  140. poll_len += 4 * chip_id - 2;
  141. }
  142. assert(spi_transfer(a1->spi_ctx, NULL, a1->spi_rx + tx_len, poll_len));
  143. hexdump("poll: RX", a1->spi_rx + tx_len, poll_len);
  144. int ack_len = tx_len + resp_len;
  145. int ack_pos = tx_len + poll_len - ack_len;
  146. hexdump("poll: ACK", a1->spi_rx + ack_pos, ack_len - 2);
  147. return (a1->spi_rx + ack_pos);
  148. }
  149. /********** A1 SPI commands */
  150. static uint8_t *cmd_BIST_FIX_BCAST(struct A1_chain *a1)
  151. {
  152. uint8_t *ret = exec_cmd(a1, A1_BIST_FIX, 0x00, NULL, 0, 0);
  153. if (ret == NULL || ret[0] != A1_BIST_FIX) {
  154. applog(LOG_ERR, "%d: cmd_BIST_FIX_BCAST failed", a1->chain_id);
  155. return NULL;
  156. }
  157. return ret;
  158. }
  159. static uint8_t *cmd_RESET_BCAST(struct A1_chain *a1, uint8_t strategy)
  160. {
  161. static uint8_t s[2];
  162. s[0] = strategy;
  163. s[1] = strategy;
  164. uint8_t *ret = exec_cmd(a1, A1_RESET, 0x00, s, 2, 0);
  165. if (ret == NULL || (ret[0] != A1_RESET && a1->num_chips != 0)) {
  166. applog(LOG_ERR, "%d: cmd_RESET_BCAST failed", a1->chain_id);
  167. return NULL;
  168. }
  169. return ret;
  170. }
  171. static uint8_t *cmd_READ_RESULT_BCAST(struct A1_chain *a1)
  172. {
  173. int tx_len = 8;
  174. memset(a1->spi_tx, 0, tx_len);
  175. a1->spi_tx[0] = A1_READ_RESULT;
  176. assert(spi_transfer(a1->spi_ctx, a1->spi_tx, a1->spi_rx, tx_len));
  177. hexdump("send: TX", a1->spi_tx, tx_len);
  178. hexdump("send: RX", a1->spi_rx, tx_len);
  179. int poll_len = tx_len + 4 * a1->num_chips;
  180. assert(spi_transfer(a1->spi_ctx, NULL, a1->spi_rx + tx_len, poll_len));
  181. hexdump("poll: RX", a1->spi_rx + tx_len, poll_len);
  182. uint8_t *scan = a1->spi_rx;
  183. int i;
  184. for (i = 0; i < poll_len; i += 2) {
  185. if ((scan[i] & 0x0f) == A1_READ_RESULT)
  186. return scan + i;
  187. }
  188. applog(LOG_ERR, "%d: cmd_READ_RESULT_BCAST failed", a1->chain_id);
  189. return NULL;
  190. }
  191. static uint8_t *cmd_WRITE_REG(struct A1_chain *a1, uint8_t chip, uint8_t *reg)
  192. {
  193. uint8_t *ret = exec_cmd(a1, A1_WRITE_REG, chip, reg, 6, 0);
  194. if (ret == NULL || ret[0] != A1_WRITE_REG) {
  195. applog(LOG_ERR, "%d: cmd_WRITE_REG failed", a1->chain_id);
  196. return NULL;
  197. }
  198. return ret;
  199. }
  200. static uint8_t *cmd_READ_REG(struct A1_chain *a1, uint8_t chip)
  201. {
  202. uint8_t *ret = exec_cmd(a1, A1_READ_REG, chip, NULL, 0, 6);
  203. if (ret == NULL || ret[0] != A1_READ_REG_RESP || ret[1] != chip) {
  204. applog(LOG_ERR, "%d: cmd_READ_REG chip %d failed",
  205. a1->chain_id, chip);
  206. return NULL;
  207. }
  208. memcpy(a1->spi_rx, ret, 8);
  209. return ret;
  210. }
  211. static uint8_t *cmd_WRITE_JOB(struct A1_chain *a1, uint8_t chip_id,
  212. uint8_t *job)
  213. {
  214. /* ensure we push the SPI command to the last chip in chain */
  215. int tx_len = WRITE_JOB_LENGTH + 2;
  216. memcpy(a1->spi_tx, job, WRITE_JOB_LENGTH);
  217. memset(a1->spi_tx + WRITE_JOB_LENGTH, 0, tx_len - WRITE_JOB_LENGTH);
  218. assert(spi_transfer(a1->spi_ctx, a1->spi_tx, a1->spi_rx, tx_len));
  219. hexdump("send: TX", a1->spi_tx, tx_len);
  220. hexdump("send: RX", a1->spi_rx, tx_len);
  221. int poll_len = 4 * chip_id - 2;
  222. assert(spi_transfer(a1->spi_ctx, NULL, a1->spi_rx + tx_len, poll_len));
  223. hexdump("poll: RX", a1->spi_rx + tx_len, poll_len);
  224. int ack_len = tx_len;
  225. int ack_pos = tx_len + poll_len - ack_len;
  226. hexdump("poll: ACK", a1->spi_rx + ack_pos, tx_len);
  227. uint8_t *ret = a1->spi_rx + ack_pos;
  228. if (ret[0] != a1->spi_tx[0] || ret[1] != a1->spi_tx[1]){
  229. applog(LOG_ERR, "%d: cmd_WRITE_JOB failed: "
  230. "0x%02x%02x/0x%02x%02x", a1->chain_id,
  231. ret[0], ret[1], a1->spi_tx[0], a1->spi_tx[1]);
  232. return NULL;
  233. }
  234. return ret;
  235. }
  236. /********** A1 low level functions */
  237. #define MAX_PLL_WAIT_CYCLES 25
  238. #define PLL_CYCLE_WAIT_TIME 40
  239. static bool check_chip_pll_lock(struct A1_chain *a1, int chip_id, uint8_t *wr)
  240. {
  241. int n;
  242. for (n = 0; n < MAX_PLL_WAIT_CYCLES; n++) {
  243. /* check for PLL lock status */
  244. if (cmd_READ_REG(a1, chip_id) && (a1->spi_rx[4] & 1) == 1)
  245. /* double check that we read back what we set before */
  246. return wr[0] == a1->spi_rx[2] && wr[1] == a1->spi_rx[3];
  247. cgsleep_ms(PLL_CYCLE_WAIT_TIME);
  248. }
  249. applog(LOG_ERR, "%d: chip %d failed PLL lock", a1->chain_id, chip_id);
  250. return false;
  251. }
  252. static uint8_t *get_pll_reg(struct A1_chain *a1, int ref_clock_khz,
  253. int sys_clock_khz)
  254. {
  255. /*
  256. * PLL parameters after:
  257. * sys_clk = (ref_clk * pll_fbdiv) / (pll_prediv * 2^(pll_postdiv - 1))
  258. *
  259. * with a higher pll_postdiv being desired over a higher pll_prediv
  260. */
  261. static uint8_t writereg[6] = { 0x00, 0x00, 0x21, 0x84, };
  262. uint8_t pre_div = 1;
  263. uint8_t post_div = 1;
  264. uint32_t fb_div;
  265. int cid = a1->chain_id;
  266. applog(LOG_WARNING, "%d: Setting PLL: CLK_REF=%dMHz, SYS_CLK=%dMHz",
  267. cid, ref_clock_khz / 1000, sys_clock_khz / 1000);
  268. /* Euclidean search for GCD */
  269. int a = ref_clock_khz;
  270. int b = sys_clock_khz;
  271. while (b != 0) {
  272. int h = a % b;
  273. a = b;
  274. b = h;
  275. }
  276. fb_div = sys_clock_khz / a;
  277. int n = ref_clock_khz / a;
  278. /* approximate multiplier if not exactly matchable */
  279. if (fb_div > 511) {
  280. int f = fb_div / n;
  281. int m = (f < 32) ? 16 : (f < 64) ? 8 :
  282. (f < 128) ? 4 : (256 < 2) ? 2 : 1;
  283. fb_div = m * fb_div / n;
  284. n = m;
  285. }
  286. /* try to maximize post divider */
  287. if ((n & 3) == 0)
  288. post_div = 3;
  289. else if ((n & 1) == 0)
  290. post_div = 2;
  291. else
  292. post_div = 1;
  293. /* remainder goes to pre_div */
  294. pre_div = n / (1 << (post_div - 1));
  295. /* correct pre_div overflow */
  296. if (pre_div > 31) {
  297. fb_div = 31 * fb_div / pre_div;
  298. pre_div = 31;
  299. }
  300. writereg[0] = (post_div << 6) | (pre_div << 1) | (fb_div >> 8);
  301. writereg[1] = fb_div & 0xff;
  302. applog(LOG_WARNING, "%d: setting PLL: pre_div=%d, post_div=%d, "
  303. "fb_div=%d: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x", cid,
  304. pre_div, post_div, fb_div,
  305. writereg[0], writereg[1], writereg[2],
  306. writereg[3], writereg[4], writereg[5]);
  307. return writereg;
  308. }
  309. static bool set_pll_config(struct A1_chain *a1, int chip_id,
  310. int ref_clock_khz, int sys_clock_khz)
  311. {
  312. uint8_t *writereg = get_pll_reg(a1, ref_clock_khz, sys_clock_khz);
  313. if (writereg == NULL)
  314. return false;
  315. if (!cmd_WRITE_REG(a1, chip_id, writereg))
  316. return false;
  317. int from = (chip_id == 0) ? 0 : chip_id - 1;
  318. int to = (chip_id == 0) ? a1->num_active_chips : chip_id - 1;
  319. int i;
  320. for (i = from; i < to; i++) {
  321. int cid = i + 1;
  322. if (!check_chip_pll_lock(a1, chip_id, writereg)) {
  323. applog(LOG_ERR, "%d: chip %d failed PLL lock",
  324. a1->chain_id, cid);
  325. return false;
  326. }
  327. }
  328. return true;
  329. }
  330. #define WEAK_CHIP_THRESHOLD 30
  331. #define BROKEN_CHIP_THRESHOLD 26
  332. #define WEAK_CHIP_SYS_CLK (600 * 1000)
  333. #define BROKEN_CHIP_SYS_CLK (400 * 1000)
  334. static bool check_chip(struct A1_chain *a1, int i)
  335. {
  336. int chip_id = i + 1;
  337. int cid = a1->chain_id;
  338. if (!cmd_READ_REG(a1, chip_id)) {
  339. applog(LOG_WARNING, "%d: Failed to read register for "
  340. "chip %d -> disabling", cid, chip_id);
  341. a1->chips[i].num_cores = 0;
  342. a1->chips[i].disabled = 1;
  343. return false;;
  344. }
  345. a1->chips[i].num_cores = a1->spi_rx[7];
  346. a1->num_cores += a1->chips[i].num_cores;
  347. applog(LOG_WARNING, "%d: Found chip %d with %d active cores",
  348. cid, chip_id, a1->chips[i].num_cores);
  349. if (a1->chips[i].num_cores < BROKEN_CHIP_THRESHOLD) {
  350. applog(LOG_WARNING, "%d: broken chip %d with %d active "
  351. "cores (threshold = %d)", cid, chip_id,
  352. a1->chips[i].num_cores, BROKEN_CHIP_THRESHOLD);
  353. set_pll_config(a1, chip_id, A1_config_options.ref_clk_khz,
  354. BROKEN_CHIP_SYS_CLK);
  355. cmd_READ_REG(a1, chip_id);
  356. hexdump_error("new.PLL", a1->spi_rx, 8);
  357. a1->chips[i].disabled = true;
  358. a1->num_cores -= a1->chips[i].num_cores;
  359. return false;
  360. }
  361. if (a1->chips[i].num_cores < WEAK_CHIP_THRESHOLD) {
  362. applog(LOG_WARNING, "%d: weak chip %d with %d active "
  363. "cores (threshold = %d)", cid,
  364. chip_id, a1->chips[i].num_cores, WEAK_CHIP_THRESHOLD);
  365. set_pll_config(a1, chip_id, A1_config_options.ref_clk_khz,
  366. WEAK_CHIP_SYS_CLK);
  367. cmd_READ_REG(a1, chip_id);
  368. hexdump_error("new.PLL", a1->spi_rx, 8);
  369. return false;
  370. }
  371. return true;
  372. }
  373. /*
  374. * BIST_START works only once after HW reset, on subsequent calls it
  375. * returns 0 as number of chips.
  376. */
  377. static int chain_detect(struct A1_chain *a1)
  378. {
  379. int tx_len = 6;
  380. memset(a1->spi_tx, 0, tx_len);
  381. a1->spi_tx[0] = A1_BIST_START;
  382. a1->spi_tx[1] = 0;
  383. if (!spi_transfer(a1->spi_ctx, a1->spi_tx, a1->spi_rx, tx_len))
  384. return 0;
  385. hexdump("TX", a1->spi_tx, 6);
  386. hexdump("RX", a1->spi_rx, 6);
  387. int i;
  388. int cid = a1->chain_id;
  389. int max_poll_words = MAX_CHAIN_LENGTH * 2;
  390. for(i = 1; i < max_poll_words; i++) {
  391. if (a1->spi_rx[0] == A1_BIST_START && a1->spi_rx[1] == 0) {
  392. spi_transfer(a1->spi_ctx, NULL, a1->spi_rx, 2);
  393. hexdump("RX", a1->spi_rx, 2);
  394. uint8_t n = a1->spi_rx[1];
  395. a1->num_chips = (i / 2) + 1;
  396. if (a1->num_chips != n) {
  397. applog(LOG_ERR, "%d: enumeration: %d <-> %d",
  398. cid, a1->num_chips, n);
  399. if (n != 0)
  400. a1->num_chips = n;
  401. }
  402. applog(LOG_WARNING, "%d: detected %d chips",
  403. cid, a1->num_chips);
  404. return a1->num_chips;
  405. }
  406. bool s = spi_transfer(a1->spi_ctx, NULL, a1->spi_rx, 2);
  407. hexdump("RX", a1->spi_rx, 2);
  408. if (!s)
  409. return 0;
  410. }
  411. applog(LOG_WARNING, "%d: no A1 chip-chain detected", cid);
  412. return 0;
  413. }
  414. /********** disable / re-enable related section (temporary for testing) */
  415. static int get_current_ms(void)
  416. {
  417. cgtimer_t ct;
  418. cgtimer_time(&ct);
  419. return cgtimer_to_ms(&ct);
  420. }
  421. static bool is_chip_disabled(struct A1_chain *a1, uint8_t chip_id)
  422. {
  423. struct A1_chip *chip = &a1->chips[chip_id - 1];
  424. return chip->disabled || chip->cooldown_begin != 0;
  425. }
  426. /* check and disable chip, remember time */
  427. static void disable_chip(struct A1_chain *a1, uint8_t chip_id)
  428. {
  429. flush_spi(a1);
  430. struct A1_chip *chip = &a1->chips[chip_id - 1];
  431. int cid = a1->chain_id;
  432. if (is_chip_disabled(a1, chip_id)) {
  433. applog(LOG_WARNING, "%d: chip %d already disabled",
  434. cid, chip_id);
  435. return;
  436. }
  437. applog(LOG_WARNING, "%d: temporary disabling chip %d", cid, chip_id);
  438. chip->cooldown_begin = get_current_ms();
  439. }
  440. /* check if disabled chips can be re-enabled */
  441. void check_disabled_chips(struct A1_chain *a1)
  442. {
  443. int i;
  444. int cid = a1->chain_id;
  445. for (i = 0; i < a1->num_active_chips; i++) {
  446. int chip_id = i + 1;
  447. struct A1_chip *chip = &a1->chips[i];
  448. if (!is_chip_disabled(a1, chip_id))
  449. continue;
  450. /* do not re-enable fully disabled chips */
  451. if (chip->disabled)
  452. continue;
  453. if (chip->cooldown_begin + COOLDOWN_MS > get_current_ms())
  454. continue;
  455. if (!cmd_READ_REG(a1, chip_id)) {
  456. chip->fail_count++;
  457. applog(LOG_WARNING, "%d: chip %d not yet working - %d",
  458. cid, chip_id, chip->fail_count);
  459. if (chip->fail_count > DISABLE_CHIP_FAIL_THRESHOLD) {
  460. applog(LOG_WARNING,
  461. "%d: completely disabling chip %d at %d",
  462. cid, chip_id, chip->fail_count);
  463. chip->disabled = true;
  464. a1->num_cores -= chip->num_cores;
  465. continue;
  466. }
  467. /* restart cooldown period */
  468. chip->cooldown_begin = get_current_ms();
  469. continue;
  470. }
  471. applog(LOG_WARNING, "%d: chip %d is working again",
  472. cid, chip_id);
  473. chip->cooldown_begin = 0;
  474. chip->fail_count = 0;
  475. }
  476. }
  477. /********** job creation and result evaluation */
  478. uint32_t get_diff(double diff)
  479. {
  480. uint32_t n_bits;
  481. int shift = 29;
  482. double f = (double) 0x0000ffff / diff;
  483. while (f < (double) 0x00008000) {
  484. shift--;
  485. f *= 256.0;
  486. }
  487. while (f >= (double) 0x00800000) {
  488. shift++;
  489. f /= 256.0;
  490. }
  491. n_bits = (int) f + (shift << 24);
  492. return n_bits;
  493. }
  494. static uint8_t *create_job(uint8_t chip_id, uint8_t job_id, struct work *work)
  495. {
  496. static uint8_t job[WRITE_JOB_LENGTH] = {
  497. /* command */
  498. 0x00, 0x00,
  499. /* midstate */
  500. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  501. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  502. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  503. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  504. /* wdata */
  505. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  506. 0x00, 0x00, 0x00, 0x00,
  507. /* start nonce */
  508. 0x00, 0x00, 0x00, 0x00,
  509. /* difficulty 1 */
  510. 0xff, 0xff, 0x00, 0x1d,
  511. /* end nonce */
  512. 0xff, 0xff, 0xff, 0xff,
  513. };
  514. uint8_t *midstate = work->midstate;
  515. uint8_t *wdata = work->data + 64;
  516. uint32_t *p1 = (uint32_t *) &job[34];
  517. uint32_t *p2 = (uint32_t *) wdata;
  518. job[0] = (job_id << 4) | A1_WRITE_JOB;
  519. job[1] = chip_id;
  520. swab256(job + 2, midstate);
  521. p1[0] = bswap_32(p2[0]);
  522. p1[1] = bswap_32(p2[1]);
  523. p1[2] = bswap_32(p2[2]);
  524. #ifdef USE_REAL_DIFF
  525. p1[4] = get_diff(work->sdiff);
  526. #endif
  527. return job;
  528. }
  529. /* set work for given chip, returns true if a nonce range was finished */
  530. static bool set_work(struct A1_chain *a1, uint8_t chip_id, struct work *work,
  531. uint8_t queue_states)
  532. {
  533. int cid = a1->chain_id;
  534. struct A1_chip *chip = &a1->chips[chip_id - 1];
  535. bool retval = false;
  536. int job_id = chip->last_queued_id + 1;
  537. applog(LOG_INFO, "%d: queuing chip %d with job_id %d, state=0x%02x",
  538. cid, chip_id, job_id, queue_states);
  539. if (job_id == (queue_states & 0x0f) || job_id == (queue_states >> 4))
  540. applog(LOG_WARNING, "%d: job overlap: %d, 0x%02x",
  541. cid, job_id, queue_states);
  542. if (chip->work[chip->last_queued_id] != NULL) {
  543. work_completed(a1->cgpu, chip->work[chip->last_queued_id]);
  544. chip->work[chip->last_queued_id] = NULL;
  545. retval = true;
  546. }
  547. uint8_t *jobdata = create_job(chip_id, job_id, work);
  548. if (!cmd_WRITE_JOB(a1, chip_id, jobdata)) {
  549. /* give back work */
  550. work_completed(a1->cgpu, work);
  551. applog(LOG_ERR, "%d: failed to set work for chip %d.%d",
  552. cid, chip_id, job_id);
  553. disable_chip(a1, chip_id);
  554. } else {
  555. chip->work[chip->last_queued_id] = work;
  556. chip->last_queued_id++;
  557. chip->last_queued_id &= 3;
  558. }
  559. return retval;
  560. }
  561. static bool get_nonce(struct A1_chain *a1, uint8_t *nonce,
  562. uint8_t *chip, uint8_t *job_id)
  563. {
  564. uint8_t *ret = cmd_READ_RESULT_BCAST(a1);
  565. if (ret == NULL)
  566. return false;
  567. if (ret[1] == 0) {
  568. applog(LOG_DEBUG, "%d: output queue empty", a1->chain_id);
  569. return false;
  570. }
  571. *job_id = ret[0] >> 4;
  572. *chip = ret[1];
  573. memcpy(nonce, ret + 2, 4);
  574. return true;
  575. }
  576. /* reset input work queues in chip chain */
  577. static bool abort_work(struct A1_chain *a1)
  578. {
  579. /* drop jobs already queued: reset strategy 0xed */
  580. return cmd_RESET_BCAST(a1, 0xed);
  581. }
  582. /********** driver interface */
  583. void exit_A1_chain(struct A1_chain *a1)
  584. {
  585. if (a1 == NULL)
  586. return;
  587. free(a1->chips);
  588. a1->chips = NULL;
  589. a1->spi_ctx = NULL;
  590. free(a1);
  591. }
  592. struct A1_chain *init_A1_chain(struct spi_ctx *ctx, int chain_id)
  593. {
  594. int i;
  595. struct A1_chain *a1 = malloc(sizeof(*a1));
  596. assert(a1 != NULL);
  597. applog(LOG_DEBUG, "%d: A1 init chain", chain_id);
  598. memset(a1, 0, sizeof(*a1));
  599. a1->spi_ctx = ctx;
  600. a1->chain_id = chain_id;
  601. a1->num_chips = chain_detect(a1);
  602. if (a1->num_chips == 0)
  603. goto failure;
  604. applog(LOG_WARNING, "spidev%d.%d: %d: Found %d A1 chips",
  605. a1->spi_ctx->config.bus, a1->spi_ctx->config.cs_line,
  606. a1->chain_id, a1->num_chips);
  607. if (!set_pll_config(a1, 0, A1_config_options.ref_clk_khz,
  608. A1_config_options.sys_clk_khz))
  609. goto failure;
  610. /* override max number of active chips if requested */
  611. a1->num_active_chips = a1->num_chips;
  612. if (A1_config_options.override_chip_num > 0 &&
  613. a1->num_chips > A1_config_options.override_chip_num) {
  614. a1->num_active_chips = A1_config_options.override_chip_num;
  615. applog(LOG_WARNING, "%d: limiting chain to %d chips",
  616. a1->chain_id, a1->num_active_chips);
  617. }
  618. a1->chips = calloc(a1->num_active_chips, sizeof(struct A1_chip));
  619. assert (a1->chips != NULL);
  620. if (!cmd_BIST_FIX_BCAST(a1))
  621. goto failure;
  622. for (i = 0; i < a1->num_active_chips; i++)
  623. check_chip(a1, i);
  624. applog(LOG_WARNING, "%d: found %d chips with total %d active cores",
  625. a1->chain_id, a1->num_active_chips, a1->num_cores);
  626. mutex_init(&a1->lock);
  627. INIT_LIST_HEAD(&a1->active_wq.head);
  628. return a1;
  629. failure:
  630. exit_A1_chain(a1);
  631. return NULL;
  632. }
  633. static bool detect_single_chain(void)
  634. {
  635. board_selector = (struct board_selector*)&dummy_board_selector;
  636. applog(LOG_WARNING, "A1: checking single chain");
  637. struct A1_chain *a1 = init_A1_chain(spi, 0);
  638. if (a1 == NULL)
  639. return false;
  640. struct cgpu_info *cgpu = malloc(sizeof(*cgpu));
  641. assert(cgpu != NULL);
  642. memset(cgpu, 0, sizeof(*cgpu));
  643. cgpu->drv = &bitmineA1_drv;
  644. cgpu->name = "BitmineA1.SingleChain";
  645. cgpu->threads = 1;
  646. cgpu->device_data = a1;
  647. a1->cgpu = cgpu;
  648. add_cgpu(cgpu);
  649. applog(LOG_WARNING, "Detected single A1 chain with %d chips / %d cores",
  650. a1->num_active_chips, a1->num_cores);
  651. return true;
  652. }
  653. bool detect_coincraft_desk(void)
  654. {
  655. static const uint8_t mcp4x_mapping[] = { 0x2c, 0x2b, 0x2a, 0x29, 0x28 };
  656. board_selector = ccd_board_selector_init();
  657. if (board_selector == NULL) {
  658. applog(LOG_INFO, "No CoinCrafd Desk backplane detected.");
  659. return false;
  660. }
  661. board_selector->reset_all();
  662. int boards_detected = 0;
  663. int board_id;
  664. for (board_id = 0; board_id < CCD_MAX_CHAINS; board_id++) {
  665. uint8_t mcp_slave = mcp4x_mapping[board_id];
  666. struct mcp4x *mcp = mcp4x_init(mcp_slave);
  667. if (mcp == NULL)
  668. continue;
  669. if (A1_config_options.wiper != 0)
  670. mcp->set_wiper(mcp, 0, A1_config_options.wiper);
  671. applog(LOG_WARNING, "checking board %d...", board_id);
  672. board_selector->select(board_id);
  673. struct A1_chain *a1 = init_A1_chain(spi, board_id);
  674. board_selector->release();
  675. if (a1 == NULL)
  676. continue;
  677. struct cgpu_info *cgpu = malloc(sizeof(*cgpu));
  678. assert(cgpu != NULL);
  679. memset(cgpu, 0, sizeof(*cgpu));
  680. cgpu->drv = &bitmineA1_drv;
  681. cgpu->name = "BitmineA1.CCD";
  682. cgpu->threads = 1;
  683. cgpu->device_data = a1;
  684. a1->cgpu = cgpu;
  685. add_cgpu(cgpu);
  686. boards_detected++;
  687. }
  688. if (boards_detected == 0)
  689. return false;
  690. applog(LOG_WARNING, "Detected CoinCraft Desk with %d boards",
  691. boards_detected);
  692. return true;
  693. }
  694. bool detect_coincraft_rig_v3(void)
  695. {
  696. board_selector = ccr_board_selector_init();
  697. if (board_selector == NULL)
  698. return false;
  699. board_selector->reset_all();
  700. int chains_detected = 0;
  701. int c;
  702. for (c = 0; c < CCR_MAX_CHAINS; c++) {
  703. applog(LOG_WARNING, "checking RIG chain %d...", c);
  704. if (!board_selector->select(c))
  705. continue;
  706. struct A1_chain *a1 = init_A1_chain(spi, c);
  707. board_selector->release();
  708. if (a1 == NULL)
  709. continue;
  710. if (A1_config_options.wiper != 0 && (c & 1) == 0) {
  711. struct mcp4x *mcp = mcp4x_init(0x28);
  712. if (mcp == NULL) {
  713. applog(LOG_ERR, "%d: Cant access poti", c);
  714. } else {
  715. mcp->set_wiper(mcp, 0, A1_config_options.wiper);
  716. mcp->set_wiper(mcp, 1, A1_config_options.wiper);
  717. mcp->exit(mcp);
  718. applog(LOG_WARNING, "%d: set wiper to 0x%02x",
  719. c, A1_config_options.wiper);
  720. }
  721. }
  722. struct cgpu_info *cgpu = malloc(sizeof(*cgpu));
  723. assert(cgpu != NULL);
  724. memset(cgpu, 0, sizeof(*cgpu));
  725. cgpu->drv = &bitmineA1_drv;
  726. cgpu->name = "BitmineA1.CCR";
  727. cgpu->threads = 1;
  728. cgpu->device_data = a1;
  729. a1->cgpu = cgpu;
  730. add_cgpu(cgpu);
  731. chains_detected++;
  732. }
  733. if (chains_detected == 0)
  734. return false;
  735. applog(LOG_WARNING, "Detected CoinCraft Rig with %d chains",
  736. chains_detected);
  737. return true;
  738. }
  739. /* Probe SPI channel and register chip chain */
  740. void A1_detect(bool hotplug)
  741. {
  742. /* no hotplug support for SPI */
  743. if (hotplug)
  744. return;
  745. /* parse bimine-a1-options */
  746. if (opt_bitmine_a1_options != NULL && parsed_config_options == NULL) {
  747. int ref_clk = 0;
  748. int sys_clk = 0;
  749. int spi_clk = 0;
  750. int override_chip_num = 0;
  751. int wiper = 0;
  752. sscanf(opt_bitmine_a1_options, "%d:%d:%d:%d:%d",
  753. &ref_clk, &sys_clk, &spi_clk, &override_chip_num,
  754. &wiper);
  755. if (ref_clk != 0)
  756. A1_config_options.ref_clk_khz = ref_clk;
  757. if (sys_clk != 0) {
  758. if (sys_clk < 100000)
  759. quit(1, "system clock must be above 100MHz");
  760. A1_config_options.sys_clk_khz = sys_clk;
  761. }
  762. if (spi_clk != 0)
  763. A1_config_options.spi_clk_khz = spi_clk;
  764. if (override_chip_num != 0)
  765. A1_config_options.override_chip_num = override_chip_num;
  766. if (wiper != 0)
  767. A1_config_options.wiper = wiper;
  768. /* config options are global, scan them once */
  769. parsed_config_options = &A1_config_options;
  770. }
  771. applog(LOG_DEBUG, "A1 detect");
  772. /* register global SPI context */
  773. struct spi_config cfg = default_spi_config;
  774. cfg.mode = SPI_MODE_1;
  775. cfg.speed = A1_config_options.spi_clk_khz * 1000;
  776. spi = spi_init(&cfg);
  777. if (spi == NULL)
  778. return;
  779. /* detect and register supported products */
  780. if (detect_coincraft_desk())
  781. return;
  782. if (detect_coincraft_rig_v3())
  783. return;
  784. if (detect_single_chain())
  785. return;
  786. /* release SPI context if no A1 products found */
  787. spi_exit(spi);
  788. }
  789. #define TEMP_UPDATE_INT_MS 2000
  790. static int64_t A1_scanwork(struct thr_info *thr)
  791. {
  792. int i;
  793. struct cgpu_info *cgpu = thr->cgpu;
  794. struct A1_chain *a1 = cgpu->device_data;
  795. int32_t nonce_ranges_processed = 0;
  796. if (a1->num_cores == 0) {
  797. cgpu->deven = DEV_DISABLED;
  798. return 0;
  799. }
  800. board_selector->select(a1->chain_id);
  801. applog(LOG_DEBUG, "A1 running scanwork");
  802. uint32_t nonce;
  803. uint8_t chip_id;
  804. uint8_t job_id;
  805. bool work_updated = false;
  806. mutex_lock(&a1->lock);
  807. if (a1->last_temp_time + TEMP_UPDATE_INT_MS < get_current_ms()) {
  808. a1->temp = board_selector->get_temp(0);
  809. a1->last_temp_time = get_current_ms();
  810. }
  811. int cid = a1->chain_id;
  812. /* poll queued results */
  813. while (true) {
  814. if (!get_nonce(a1, (uint8_t*)&nonce, &chip_id, &job_id))
  815. break;
  816. nonce = bswap_32(nonce);
  817. work_updated = true;
  818. if (chip_id < 1 || chip_id > a1->num_active_chips) {
  819. applog(LOG_WARNING, "%d: wrong chip_id %d",
  820. cid, chip_id);
  821. continue;
  822. }
  823. if (job_id < 1 && job_id > 4) {
  824. applog(LOG_WARNING, "%d: chip %d: result has wrong "
  825. "job_id %d", cid, chip_id, job_id);
  826. flush_spi(a1);
  827. continue;
  828. }
  829. struct A1_chip *chip = &a1->chips[chip_id - 1];
  830. struct work *work = chip->work[job_id - 1];
  831. if (work == NULL) {
  832. /* already been flushed => stale */
  833. applog(LOG_WARNING, "%d: chip %d: stale nonce 0x%08x",
  834. cid, chip_id, nonce);
  835. chip->stales++;
  836. continue;
  837. }
  838. if (!submit_nonce(thr, work, nonce)) {
  839. applog(LOG_WARNING, "%d: chip %d: invalid nonce 0x%08x",
  840. cid, chip_id, nonce);
  841. chip->hw_errors++;
  842. /* add a penalty of a full nonce range on HW errors */
  843. nonce_ranges_processed--;
  844. continue;
  845. }
  846. applog(LOG_DEBUG, "YEAH: %d: chip %d / job_id %d: nonce 0x%08x",
  847. cid, chip_id, job_id, nonce);
  848. chip->nonces_found++;
  849. }
  850. /* check for completed works */
  851. for (i = a1->num_active_chips; i > 0; i--) {
  852. uint8_t c = i;
  853. if (is_chip_disabled(a1, c))
  854. continue;
  855. if (!cmd_READ_REG(a1, c)) {
  856. disable_chip(a1, c);
  857. continue;
  858. }
  859. uint8_t qstate = a1->spi_rx[5] & 3;
  860. uint8_t qbuff = a1->spi_rx[6];
  861. struct work *work;
  862. struct A1_chip *chip = &a1->chips[i - 1];
  863. switch(qstate) {
  864. case 3:
  865. continue;
  866. case 2:
  867. applog(LOG_ERR, "%d: chip %d: invalid state = 2",
  868. cid, c);
  869. continue;
  870. case 1:
  871. /* fall through */
  872. case 0:
  873. work_updated = true;
  874. work = wq_dequeue(&a1->active_wq);
  875. if (work == NULL) {
  876. applog(LOG_INFO, "%d: chip %d: work underflow",
  877. cid, c);
  878. break;
  879. }
  880. if (set_work(a1, c, work, qbuff)) {
  881. chip->nonce_ranges_done++;
  882. nonce_ranges_processed++;
  883. }
  884. applog(LOG_DEBUG, "%d: chip %d: job done: %d/%d/%d/%d",
  885. cid, c,
  886. chip->nonce_ranges_done, chip->nonces_found,
  887. chip->hw_errors, chip->stales);
  888. break;
  889. }
  890. }
  891. check_disabled_chips(a1);
  892. mutex_unlock(&a1->lock);
  893. board_selector->release();
  894. if (nonce_ranges_processed < 0)
  895. nonce_ranges_processed = 0;
  896. if (nonce_ranges_processed != 0) {
  897. applog(LOG_DEBUG, "%d, nonces processed %d",
  898. cid, nonce_ranges_processed);
  899. }
  900. /* in case of no progress, prevent busy looping */
  901. if (!work_updated)
  902. cgsleep_ms(40);
  903. return (int64_t)nonce_ranges_processed << 32;
  904. }
  905. /* queue two work items per chip in chain */
  906. static bool A1_queue_full(struct cgpu_info *cgpu)
  907. {
  908. struct A1_chain *a1 = cgpu->device_data;
  909. int queue_full = false;
  910. mutex_lock(&a1->lock);
  911. applog(LOG_DEBUG, "%d, A1 running queue_full: %d/%d",
  912. a1->chain_id, a1->active_wq.num_elems, a1->num_active_chips);
  913. if (a1->active_wq.num_elems >= a1->num_active_chips * 2)
  914. queue_full = true;
  915. else
  916. wq_enqueue(&a1->active_wq, get_queued(cgpu));
  917. mutex_unlock(&a1->lock);
  918. return queue_full;
  919. }
  920. static void A1_flush_work(struct cgpu_info *cgpu)
  921. {
  922. struct A1_chain *a1 = cgpu->device_data;
  923. int cid = a1->chain_id;
  924. board_selector->select(cid);
  925. applog(LOG_DEBUG, "%d: A1 running flushwork", cid);
  926. int i;
  927. mutex_lock(&a1->lock);
  928. /* stop chips hashing current work */
  929. if (!abort_work(a1)) {
  930. applog(LOG_ERR, "%d: failed to abort work in chip chain!", cid);
  931. }
  932. /* flush the work chips were currently hashing */
  933. for (i = 0; i < a1->num_active_chips; i++) {
  934. int j;
  935. struct A1_chip *chip = &a1->chips[i];
  936. for (j = 0; j < 4; j++) {
  937. struct work *work = chip->work[j];
  938. if (work == NULL)
  939. continue;
  940. applog(LOG_DEBUG, "%d: flushing chip %d, work %d: 0x%p",
  941. cid, i, j + 1, work);
  942. work_completed(cgpu, work);
  943. chip->work[j] = NULL;
  944. }
  945. chip->last_queued_id = 0;
  946. }
  947. /* flush queued work */
  948. applog(LOG_DEBUG, "%d: flushing queued work...", cid);
  949. while (a1->active_wq.num_elems > 0) {
  950. struct work *work = wq_dequeue(&a1->active_wq);
  951. assert(work != NULL);
  952. work_completed(cgpu, work);
  953. }
  954. mutex_unlock(&a1->lock);
  955. board_selector->release();
  956. }
  957. static void A1_get_statline_before(char *buf, size_t len,
  958. struct cgpu_info *cgpu)
  959. {
  960. struct A1_chain *a1 = cgpu->device_data;
  961. char temp[10];
  962. if (a1->temp != 0)
  963. snprintf(temp, 9, "%2dC", a1->temp);
  964. tailsprintf(buf, len, " %2d:%2d/%3d %s",
  965. a1->chain_id, a1->num_active_chips, a1->num_cores,
  966. a1->temp == 0 ? " " : temp);
  967. }
  968. struct device_drv bitmineA1_drv = {
  969. .drv_id = DRIVER_bitmineA1,
  970. .dname = "BitmineA1",
  971. .name = "BA1",
  972. .drv_detect = A1_detect,
  973. .hash_work = hash_queued_work,
  974. .scanwork = A1_scanwork,
  975. .queue_full = A1_queue_full,
  976. .flush_work = A1_flush_work,
  977. .get_statline_before = A1_get_statline_before,
  978. };