driver-aan.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * Copyright 2014 Luke Dashjr
  3. * Copyright 2013 Zefir Kurtisi
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <stdbool.h>
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include "deviceapi.h"
  15. #include "driver-aan.h"
  16. #include "logging.h"
  17. #include "lowl-spi.h"
  18. #include "miner.h"
  19. #include "util.h"
  20. #define AAN_DEFAULT_NONCE_PDIFF 8
  21. // WARNING: Do not just change this without fixing aan_freq2pll!
  22. #define AAN_MAX_FREQ 6132
  23. #define AAN_PROBE_TIMEOUT_US 3750000
  24. #define AAN_INIT_TIMEOUT_US 5000000
  25. #define AAN_READ_INTERVAL_US 100000
  26. #define AAN_REGISTER_SIZE 6
  27. enum aan_cmd {
  28. AAN_BIST_START = 0x01,
  29. AAN_BIST_FIX = 0x03,
  30. AAN_RESET = 0x04,
  31. AAN_WRITE_JOB = 0x07,
  32. AAN_READ_RESULT = 0x08,
  33. AAN_WRITE_REG = 0x09,
  34. AAN_READ_REG = 0x0a,
  35. AAN_READ_REG_RESP = 0x1a,
  36. };
  37. static
  38. unsigned aan_pll2freq(const uint16_t pll)
  39. {
  40. const uint8_t pll_postdiv = (pll >> 0xe);
  41. const uint8_t pll_prediv = (pll >> 9) & 0x1f;
  42. const uint16_t pll_fbdiv = pll & 0x1ff;
  43. return (12 * pll_fbdiv) / pll_prediv / (1 << (pll_postdiv - 1));
  44. }
  45. static
  46. uint16_t aan_freq2pll(unsigned freq)
  47. {
  48. retry: ;
  49. uint8_t postdiv = 3, prediv = 3;
  50. uint16_t fbdiv = freq / 3;
  51. if (fbdiv * 3 == freq)
  52. prediv = 1;
  53. else
  54. fbdiv = freq;
  55. if (!(fbdiv & 3))
  56. {
  57. fbdiv >>= 2;
  58. postdiv = 1;
  59. }
  60. else
  61. if (!(fbdiv & 1))
  62. {
  63. fbdiv >>= 1;
  64. postdiv = 2;
  65. }
  66. if (fbdiv > 0x1ff)
  67. {
  68. --freq;
  69. goto retry;
  70. }
  71. const uint16_t pll = (((postdiv << 5) | prediv) << 9) | fbdiv;
  72. return pll;
  73. }
  74. static
  75. void _test_aan_pll(const unsigned expect, const uint8_t postdiv, const uint8_t prediv, const uint16_t fbdiv)
  76. {
  77. const uint16_t pll = (((postdiv << 5) | prediv) << 9) | fbdiv;
  78. const unsigned got = aan_pll2freq(pll);
  79. if (got != expect)
  80. {
  81. ++unittest_failures;
  82. applog(LOG_WARNING, "%s test failed for %4u(%x,%02x,%3d): got %4u", "aan_pll2freq", expect, postdiv, prediv, fbdiv, got);
  83. }
  84. }
  85. static
  86. void _test_aan_pll2(const unsigned freq)
  87. {
  88. const uint16_t pll = aan_freq2pll(freq);
  89. const unsigned got = aan_pll2freq(pll);
  90. if (got / 12 != freq / 12)
  91. {
  92. ++unittest_failures;
  93. const uint8_t postdiv = (pll >> 0xe);
  94. const uint8_t prediv = (pll >> 9) & 0x1f;
  95. const uint16_t fbdiv = pll & 0x1ff;
  96. applog(LOG_WARNING, "%s test failed for %4u: got %4u(%x,%02x,%3d)", "aan_freq2pll", freq, got, postdiv, prediv, fbdiv);
  97. }
  98. }
  99. void test_aan_pll(void)
  100. {
  101. _test_aan_pll(1000, 0b01,0b00011,0b011111010);
  102. _test_aan_pll( 950, 0b10,0b00011,0b111011011);
  103. _test_aan_pll( 900, 0b01,0b00001,0b001001011);
  104. _test_aan_pll( 850, 0b10,0b00011,0b110101001);
  105. _test_aan_pll( 800, 0b01,0b00011,0b011001000);
  106. _test_aan_pll( 750, 0b10,0b00001,0b001111101);
  107. _test_aan_pll( 700, 0b01,0b00011,0b010101111);
  108. _test_aan_pll( 650, 0b10,0b00011,0b101000101);
  109. _test_aan_pll( 600, 0b01,0b00001,0b000110010);
  110. _test_aan_pll( 550, 0b10,0b00011,0b100010011);
  111. _test_aan_pll( 500, 0b10,0b00011,0b011111010);
  112. _test_aan_pll( 100, 0b11,0b00011,0b001100100);
  113. for (unsigned i = 1; i <= AAN_MAX_FREQ; ++i)
  114. _test_aan_pll2(i);
  115. }
  116. static void aan_spi_parse_rx(struct spi_port *);
  117. static
  118. void aan_spi_cmd_queue(struct spi_port * const spi, const uint8_t cmd, const uint8_t chip, const void * const data, const size_t datalen)
  119. {
  120. const struct aan_hooks * const hooks = spi->userp;
  121. const uint8_t cmdbuf[2] = {cmd, chip};
  122. hooks->precmd(spi);
  123. spi_emit_buf(spi, cmdbuf, sizeof(cmdbuf));
  124. if (datalen)
  125. spi_emit_buf(spi, data, datalen);
  126. }
  127. static
  128. bool aan_spi_txrx(struct spi_port * const spi)
  129. {
  130. if (unlikely(!spi_txrx(spi)))
  131. return false;
  132. aan_spi_parse_rx(spi);
  133. return true;
  134. }
  135. static
  136. bool aan_spi_cmd_send(struct spi_port * const spi, const uint8_t cmd, const uint8_t chip, const void * const data, const size_t datalen)
  137. {
  138. aan_spi_cmd_queue(spi, cmd, chip, data, datalen);
  139. return aan_spi_txrx(spi);
  140. }
  141. static
  142. bool aan_spi_cmd_resp(struct spi_port * const spi, const uint8_t cmd, const uint8_t chip, const struct timeval * const tvp_timeout)
  143. {
  144. const uint8_t cmdbuf[2] = {cmd, chip};
  145. uint8_t * const rx = spi_getrxbuf(spi);
  146. while (true)
  147. {
  148. spi_emit_nop(spi, 2);
  149. if (unlikely(!spi_txrx(spi)))
  150. return false;
  151. if (!memcmp(rx, cmdbuf, 2))
  152. break;
  153. aan_spi_parse_rx(spi);
  154. if (unlikely(tvp_timeout && timer_passed(tvp_timeout, NULL)))
  155. return false;
  156. }
  157. spi_clear_buf(spi);
  158. return true;
  159. }
  160. static
  161. bool aan_spi_cmd(struct spi_port * const spi, const uint8_t cmd, const uint8_t chip, const void * const data, const size_t datalen, const struct timeval * const tvp_timeout)
  162. {
  163. if (!aan_spi_cmd_send(spi, cmd, chip, data, datalen))
  164. return false;
  165. if (!aan_spi_cmd_resp(spi, cmd, chip, tvp_timeout))
  166. return false;
  167. return true;
  168. }
  169. bool aan_read_reg_direct(struct spi_port * const spi, const uint8_t chip, void * const out_buf, const struct timeval * const tvp_timeout)
  170. {
  171. if (!aan_spi_cmd_send(spi, AAN_READ_REG, chip, NULL, 0))
  172. return false;
  173. if (!aan_spi_cmd_resp(spi, AAN_READ_REG_RESP, chip, tvp_timeout))
  174. return false;
  175. spi_emit_nop(spi, AAN_REGISTER_SIZE);
  176. if (!spi_txrx(spi))
  177. applogr(false, LOG_DEBUG, "%s: %s failed", __func__, "spi_txrx");
  178. uint8_t * const rx = spi_getrxbuf(spi);
  179. memcpy(out_buf, rx, AAN_REGISTER_SIZE);
  180. return true;
  181. }
  182. static inline
  183. bool aan_read_reg(struct spi_port * const spi, const uint8_t chip, void * const out_buf, const struct timeval * const tvp_timeout)
  184. {
  185. const struct aan_hooks * const hooks = spi->userp;
  186. return hooks->read_reg(spi, chip, out_buf, tvp_timeout);
  187. }
  188. int aan_detect_spi(int * const out_chipcount, struct spi_port * const * const spi_a, const int spi_n)
  189. {
  190. struct timeval tv_timeout;
  191. timer_set_delay_from_now(&tv_timeout, AAN_PROBE_TIMEOUT_US);
  192. int state[spi_n];
  193. int completed = 0;
  194. for (int i = 0; i < spi_n; ++i)
  195. {
  196. struct spi_port * const spi = spi_a[i];
  197. aan_spi_cmd_send(spi, state[i] = AAN_RESET, AAN_ALL_CHIPS, NULL, 0);
  198. out_chipcount[i] = -1;
  199. }
  200. do {
  201. for (int i = 0; i < spi_n; ++i)
  202. {
  203. if (state[i] == -1)
  204. continue;
  205. struct spi_port * const spi = spi_a[i];
  206. spi_emit_nop(spi, 2);
  207. if (unlikely(!spi_txrx(spi)))
  208. {
  209. spifail:
  210. state[i] = -1;
  211. continue;
  212. }
  213. uint8_t * const rx = spi_getrxbuf(spi);
  214. if (rx[0] == state[i] && rx[1] == AAN_ALL_CHIPS)
  215. {
  216. switch (state[i])
  217. {
  218. case AAN_RESET:
  219. applog(LOG_DEBUG, "%s: Reset complete", spi->repr);
  220. spi_clear_buf(spi);
  221. aan_spi_cmd_send(spi, state[i] = AAN_BIST_START, AAN_ALL_CHIPS, NULL, 0);
  222. spi_emit_nop(spi, 2);
  223. break;
  224. case AAN_BIST_START:
  225. if (unlikely(!spi_txrx(spi)))
  226. goto spifail;
  227. out_chipcount[i] = rx[1];
  228. state[i] = -1;
  229. ++completed;
  230. applog(LOG_DEBUG, "%s: BIST_START complete (%d chips)", spi->repr, rx[1]);
  231. break;
  232. }
  233. spi_clear_buf(spi);
  234. continue;
  235. }
  236. aan_spi_parse_rx(spi);
  237. }
  238. } while (completed < spi_n && likely(!timer_passed(&tv_timeout, NULL)));
  239. applog(LOG_DEBUG, "%s completed for %d out of %d SPI ports", __func__, completed, spi_n);
  240. return completed;
  241. }
  242. bool aan_init(struct thr_info * const master_thr)
  243. {
  244. struct cgpu_info * const master_dev = master_thr->cgpu, *dev = NULL;
  245. struct aan_board_data *board = NULL;
  246. struct timeval tv_timeout, tv_now;
  247. int chipid = 0;
  248. for_each_managed_proc(proc, master_dev)
  249. {
  250. struct spi_port * const spi = proc->device_data;
  251. struct thr_info * const thr = proc->thr[0];
  252. if (dev != proc->device)
  253. {
  254. dev = proc->device;
  255. chipid = 0;
  256. timer_set_now(&tv_now);
  257. board = malloc(sizeof(*board));
  258. *board = (struct aan_board_data){
  259. .master_dev = master_dev,
  260. .spi = spi,
  261. .tv_next_poll = tv_now,
  262. };
  263. spi->cgpu = dev;
  264. while (true)
  265. {
  266. timer_set_delay(&tv_timeout, &tv_now, AAN_INIT_TIMEOUT_US);
  267. if (aan_spi_cmd(spi, AAN_BIST_FIX, AAN_ALL_CHIPS, NULL, 0, &tv_timeout))
  268. break;
  269. applog(LOG_ERR, "%s: Failed to %s", proc->dev_repr, "BIST_FIX");
  270. }
  271. }
  272. proc->device_data = board;
  273. struct aan_chip_data * const chip = malloc(sizeof(*chip));
  274. thr->cgpu_data = chip;
  275. thr->queue_full = true;
  276. *chip = (struct aan_chip_data){
  277. .chipid = ++chipid,
  278. .desired_nonce_pdiff = AAN_DEFAULT_NONCE_PDIFF,
  279. .desired_pllreg = 0x87a9, // 850 MHz
  280. };
  281. cgpu_set_defaults(proc);
  282. }
  283. master_thr->tv_poll = tv_now;
  284. return true;
  285. }
  286. static
  287. bool aan_spi_send_work(struct spi_port * const spi, const uint8_t chipid, const uint8_t jobid, const struct work * const work)
  288. {
  289. uint8_t buf[0x38];
  290. swab256(&buf[0], work->midstate);
  291. swap32yes(&buf[0x20], &work->data[0x40], 3);
  292. memset(&buf[0x2c], 0, 4); // start nonce
  293. uint32_t compressed_target = (uint32_t)(0x10000 / work->nonce_diff) | (/*exponent*/ 0x1d << 24);
  294. pk_u32le(buf, 0x30, compressed_target);
  295. memset(&buf[0x34], 0xff, 4); // end nonce
  296. return aan_spi_cmd_send(spi, AAN_WRITE_JOB | (jobid << 4), chipid, buf, sizeof(buf));
  297. }
  298. static bool set_work(struct cgpu_info *, uint8_t, struct work *);
  299. bool aan_queue_append(struct thr_info * const thr, struct work * const work)
  300. {
  301. struct cgpu_info *proc = thr->cgpu;
  302. struct aan_chip_data * const chip = thr->cgpu_data;
  303. struct cgpu_info *dev = proc->device;
  304. struct aan_board_data *board = dev->device_data;
  305. struct cgpu_info * const master_dev = board->master_dev;
  306. struct aan_board_data * const master_board = master_dev->device_data;
  307. applog(LOG_DEBUG, "%s: queue_append queues_empty=%d", proc->proc_repr, master_board->queues_empty-1);
  308. work->nonce_diff = work->work_difficulty;
  309. if (work->nonce_diff > chip->desired_nonce_pdiff)
  310. work->nonce_diff = chip->desired_nonce_pdiff;
  311. chip->current_nonce_pdiff = work->nonce_diff;
  312. if (set_work(dev, proc->proc_id + 1, work))
  313. hashes_done2(thr, 0x100000000, NULL);
  314. thr->queue_full = true;
  315. if (!--master_board->queues_empty)
  316. {
  317. struct thr_info * const master_thr = master_dev->thr[0];
  318. // Reactivate polling
  319. dev = NULL;
  320. for_each_managed_proc(proc, master_dev)
  321. {
  322. if (dev == proc->device)
  323. continue;
  324. dev = proc->device;
  325. board = dev->device_data;
  326. reduce_timeout_to(&master_thr->tv_poll, &board->tv_next_poll);
  327. }
  328. }
  329. return true;
  330. }
  331. void aan_queue_flush(struct thr_info * const thr)
  332. {
  333. // TODO
  334. }
  335. struct cgpu_info *aan_proc_for_chipid(struct cgpu_info * const dev, const int chipid)
  336. {
  337. struct cgpu_info *proc = dev;
  338. for (int i = 1; i < chipid; ++i)
  339. {
  340. proc = proc->next_proc;
  341. if (unlikely((!proc) || proc->device != dev))
  342. {
  343. badchipid:
  344. inc_hw_errors_only(dev->thr[0]);
  345. applogr(NULL, LOG_ERR, "%s: Chip number %d out of range", dev->dev_repr, chipid);
  346. }
  347. }
  348. if (unlikely(!chipid))
  349. goto badchipid;
  350. return proc;
  351. }
  352. static
  353. void aan_spi_parse_rx(struct spi_port * const spi)
  354. {
  355. spi_clear_buf(spi);
  356. }
  357. #define MAX_POLL_NUM 20
  358. /* set work for given chip, returns true if a nonce range was finished */
  359. static
  360. bool set_work(struct cgpu_info * const dev, const uint8_t chip_id, struct work * const work)
  361. {
  362. struct aan_board_data * const board = dev->device_data;
  363. struct spi_port * const spi = board->spi;
  364. struct cgpu_info * const proc = aan_proc_for_chipid(dev, chip_id);
  365. struct thr_info * const thr = proc->thr[0];
  366. struct aan_chip_data * const chip = thr->cgpu_data;
  367. bool retval = false;
  368. ++chip->last_jobid;
  369. chip->last_jobid &= 3;
  370. if (chip->works[chip->last_jobid] != NULL)
  371. {
  372. free_work(chip->works[chip->last_jobid]);
  373. chip->works[chip->last_jobid] = NULL;
  374. retval = true;
  375. }
  376. if (!aan_spi_send_work(spi, chip_id, chip->last_jobid + 1, work))
  377. {
  378. free_work(work);
  379. applog(LOG_ERR, "%"PRIpreprv": Failed to set work %d", proc->proc_repr, chip->last_jobid + 1);
  380. }
  381. else
  382. chip->works[chip->last_jobid] = work;
  383. spi_clear_buf(spi);
  384. return retval;
  385. }
  386. /* check for pending results in a chain, returns false if output queue empty */
  387. static
  388. bool get_nonce(struct cgpu_info * const dev, uint8_t * const nonce, uint8_t * const chip, uint8_t * const job_id)
  389. {
  390. struct aan_board_data * const board = dev->device_data;
  391. struct spi_port * const spi = board->spi;
  392. int pollLen = MAX_POLL_NUM * dev->procs;
  393. if (pollLen <= 0)
  394. pollLen = MAX_POLL_NUM;
  395. if (!aan_spi_cmd_send(spi, AAN_READ_RESULT, AAN_ALL_CHIPS, NULL, 0))
  396. return false;
  397. for (int i = 0; i < pollLen; ++i)
  398. {
  399. spi_clear_buf(spi);
  400. spi_emit_nop(spi, 2);
  401. if (!spi_txrx(spi))
  402. applogr(false, LOG_ERR, "%s: SPI error in get_nonce", dev->dev_repr);
  403. uint8_t * const spi_rx = spi_getrxbuf(spi);
  404. if (spi_rx[0] == AAN_READ_RESULT && spi_rx[1] == 0x00)
  405. applogr(false, LOG_DEBUG, "%s: Output queue empty", dev->dev_repr);
  406. if ((spi_rx[0] & 0x0f) == AAN_READ_RESULT && spi_rx[1] != 0)
  407. {
  408. *job_id = spi_rx[0] >> 4;
  409. *chip = spi_rx[1];
  410. spi_emit_nop(spi, 2);
  411. if (!spi_txrx(spi))
  412. applogr(false, LOG_ERR, "SPI Err(%s):get_nonce", dev->dev_repr);
  413. memcpy(nonce, spi_rx, 4);
  414. applog(LOG_DEBUG, "%s: Got nonce for chip %d / job_id %d", dev->dev_repr, *chip, *job_id);
  415. return true;
  416. }
  417. }
  418. return false;
  419. }
  420. static
  421. void aan_scanwork(struct cgpu_info * const dev, struct thr_info * const master_thr)
  422. {
  423. struct aan_board_data * const board = dev->device_data;
  424. struct spi_port * const spi = board->spi;
  425. uint32_t nonce;
  426. uint8_t chip_id;
  427. uint8_t job_id;
  428. bool work_updated = false;
  429. if (!timer_passed(&board->tv_next_poll, NULL))
  430. goto out;
  431. while (get_nonce(dev, (uint8_t*)&nonce, &chip_id, &job_id))
  432. {
  433. nonce = bswap_32(nonce);
  434. work_updated = true;
  435. struct cgpu_info * const proc = aan_proc_for_chipid(dev, chip_id);
  436. if (!proc)
  437. continue;
  438. struct thr_info * const thr = proc->thr[0];
  439. struct aan_chip_data * const chip = thr->cgpu_data;
  440. if (job_id < 1 || job_id > 4)
  441. {
  442. badjob:
  443. inc_hw_errors3(thr, NULL, &nonce, chip->current_nonce_pdiff);
  444. continue;
  445. }
  446. struct work * const work = chip->works[job_id - 1];
  447. if (!work)
  448. goto badjob;
  449. submit_nonce(thr, work, nonce);
  450. }
  451. /* check for completed works */
  452. for_each_logical_proc(proc, dev)
  453. {
  454. struct thr_info * const thr = proc->thr[0];
  455. struct aan_chip_data * const chip = thr->cgpu_data;
  456. const int i = proc->proc_id;
  457. uint8_t reg[AAN_REGISTER_SIZE];
  458. if (!aan_read_reg(spi, i + 1, reg, NULL))
  459. {
  460. applog(LOG_ERR, "%"PRIpreprv": Failed to read reg", proc->proc_repr);
  461. continue;
  462. }
  463. const uint16_t pllreg = upk_u16be(reg, 0);
  464. chip->current_pllreg = pllreg;
  465. if (pllreg != chip->desired_pllreg)
  466. {
  467. // Wait for chip to idle before changing register
  468. if (!(reg[3] & 3))
  469. {
  470. applog(LOG_DEBUG, "%"PRIpreprv": Asserting PLL change: %04x->%04x", proc->proc_repr, pllreg, chip->desired_pllreg);
  471. uint8_t regset[AAN_REGISTER_SIZE];
  472. memcpy(&regset[2], &reg[2], AAN_REGISTER_SIZE - 2);
  473. pk_u16be(regset, 0, chip->desired_pllreg);
  474. aan_spi_cmd_send(spi, AAN_WRITE_REG, chip->chipid, regset, AAN_REGISTER_SIZE);
  475. }
  476. }
  477. else
  478. if ((reg[3] & 2) != 2)
  479. {
  480. struct cgpu_info * const master_dev = board->master_dev;
  481. struct aan_board_data * const master_board = master_dev->device_data;
  482. work_updated = true;
  483. thr->queue_full = false;
  484. ++master_board->queues_empty;
  485. applog(LOG_DEBUG, "%s: queue_full=false queues_empty=%d", proc->proc_repr, master_board->queues_empty);
  486. }
  487. }
  488. if (!work_updated)
  489. timer_set_delay_from_now(&board->tv_next_poll, AAN_READ_INTERVAL_US);
  490. out:
  491. reduce_timeout_to(&master_thr->tv_poll, &board->tv_next_poll);
  492. }
  493. void aan_poll(struct thr_info * const master_thr)
  494. {
  495. struct cgpu_info * const master_dev = master_thr->cgpu, *dev = NULL;
  496. struct aan_board_data * const master_board = master_dev->device_data;
  497. timer_unset(&master_thr->tv_poll);
  498. for_each_managed_proc(proc, master_dev)
  499. {
  500. if (dev == proc->device)
  501. continue;
  502. dev = proc->device;
  503. aan_scanwork(dev, master_thr);
  504. }
  505. if (master_board->queues_empty)
  506. // Avoid polling when we have queues to fill
  507. timer_unset(&master_thr->tv_poll);
  508. }
  509. const char *aan_set_clock(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const success)
  510. {
  511. struct thr_info * const thr = proc->thr[0];
  512. struct aan_chip_data * const chip = thr->cgpu_data;
  513. if (newvalue[0] == 'x')
  514. {
  515. char *p;
  516. chip->desired_pllreg = strtol(&newvalue[1], &p, 0x10);
  517. if (p != &newvalue[5])
  518. return "Invalid hex PLL data";
  519. }
  520. else
  521. {
  522. const int nv = atoi(newvalue);
  523. if (nv <= 0 || nv > AAN_MAX_FREQ)
  524. return "Invalid clock frequency";
  525. chip->desired_pllreg = aan_freq2pll(nv);
  526. }
  527. return NULL;
  528. }
  529. const char *aan_set_diff(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const success)
  530. {
  531. struct thr_info * const thr = proc->thr[0];
  532. struct aan_chip_data * const chip = thr->cgpu_data;
  533. const double nv = atof(newvalue);
  534. if (nv <= 0)
  535. return "Invalid difficulty";
  536. chip->desired_nonce_pdiff = nv;
  537. return NULL;
  538. }
  539. struct api_data *aan_api_device_status(struct cgpu_info * const proc)
  540. {
  541. struct thr_info * const thr = proc->thr[0];
  542. struct aan_chip_data * const chip = thr->cgpu_data;
  543. struct api_data *root = NULL;
  544. double mhz = aan_pll2freq(chip->current_pllreg);
  545. root = api_add_freq(root, "Frequency", &mhz, true);
  546. return root;
  547. }
  548. const struct bfg_set_device_definition aan_set_device_funcs[] = {
  549. {"clock", aan_set_clock, "clock frequency (MHz)"},
  550. {"diff", aan_set_diff, "desired nonce difficulty"},
  551. {NULL},
  552. };