driver-aan.c 17 KB

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