driver-alchemist.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Copyright 2015 John Stefanopoulos
  3. * Copyright 2014-2015 Luke Dashjr
  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. // THIS DRIVER REQUIRES THE LATEST FIRMWARE FOR AlCHEMINERS DEVELOPED BY JSTEFANOP
  11. // IT WILL NOT WORK WITH THE STOCK FACTORY FIRMWARE ON THE BOARDS
  12. // PLEASE CONTACT JSTEFANOP AT MAC DOT COM OR JSTEFANOP ON LITECOINTALK DOT ORG FOR MORE INFO
  13. #include "config.h"
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. #include <fcntl.h>
  21. #include "deviceapi.h"
  22. #include "logging.h"
  23. #include "lowlevel.h"
  24. #include "lowl-vcom.h"
  25. #include "util.h"
  26. static const uint8_t alchemist_max_chips = 0x20;
  27. #define ALCHEMIST_DEFAULT_FREQUENCY 352
  28. #define ALCHEMIST_MIN_CLOCK 200
  29. #define ALCHEMIST_MAX_CLOCK 400
  30. // Number of seconds full board of 1728 cores @ 352mhz takes to scan full range
  31. #define ALCHEMIST_HASH_SPEED 134.0
  32. #define ALCHEMIST_MAX_NONCE 0xffffffff
  33. #define ALCHEMIST_READ_SIZE 9
  34. #define alchemist_max_clusters_per_chip 6
  35. #define alchemist_max_cores_per_cluster 9
  36. static const uint8_t alchemist_g_head[] = {
  37. 0xd4, 0x59, 0x2d, 0x01, 0x1d, 0x01, 0x8e, 0xa7, 0x4e, 0xbb, 0x17, 0xb8, 0x06, 0x6b, 0x2a, 0x75,
  38. 0x83, 0x99, 0xd5, 0xf1, 0x9b, 0x5c, 0x60, 0x73, 0xd0, 0x9b, 0x50, 0x0d, 0x92, 0x59, 0x82, 0xad,
  39. 0xc4, 0xb3, 0xed, 0xd3, 0x52, 0xef, 0xe1, 0x46, 0x67, 0xa8, 0xca, 0x9f, 0x27, 0x9f, 0x63, 0x30,
  40. 0xcc, 0xbb, 0xb9, 0x10, 0x3b, 0x9e, 0x3a, 0x53, 0x50, 0x76, 0x50, 0x52, 0x08, 0x1d, 0xdb, 0xae,
  41. 0x89, 0x8f, 0x1e, 0xf6, 0xb8, 0xc6, 0x4f, 0x3b, 0xce, 0xf7, 0x15, 0xf6, 0, 0, 0, 1,
  42. 0, 0, 0, 1, 0x8e, 0xa7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 0
  44. };
  45. BFG_REGISTER_DRIVER(alchemist_drv)
  46. static const struct bfg_set_device_definition alchemist_set_device_funcs_probe[];
  47. struct alchemist_chip {
  48. uint8_t chipid;
  49. uint8_t global_reg[8];
  50. uint16_t chip_mask[alchemist_max_clusters_per_chip];
  51. uint32_t clst_offset[alchemist_max_clusters_per_chip];
  52. unsigned active_cores;
  53. unsigned freq;
  54. };
  55. static
  56. void alchemist_chip_init(struct alchemist_chip * const chip, const uint8_t chipid)
  57. {
  58. *chip = (struct alchemist_chip){
  59. .chipid = chipid,
  60. .global_reg = {0, 4, 0x40, 0, 0, 0, 0, 1},
  61. .chip_mask = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
  62. .clst_offset = {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},
  63. .active_cores = 1728,
  64. .freq = ALCHEMIST_DEFAULT_FREQUENCY,
  65. };
  66. }
  67. static
  68. void alchemist_reset_board(const char * const devpath)
  69. {
  70. char gpio[8];
  71. int fd;
  72. char buf[50];
  73. // TODO: allow custom reset pin through --set for non-stock controller
  74. if (strcmp(devpath, "/dev/ttyO1") == 0)
  75. strcpy(gpio, "gpio117");
  76. else if (strcmp(devpath, "/dev/ttyO2") == 0)
  77. strcpy(gpio, "gpio110");
  78. else if (strcmp(devpath, "/dev/ttyO3") == 0)
  79. strcpy(gpio, "gpio111");
  80. else if (strcmp(devpath, "/dev/ttyO4") == 0)
  81. strcpy(gpio, "gpio112");
  82. else if (strcmp(devpath, "/dev/ttyUSB0") == 0)
  83. strcpy(gpio, "gpio113");
  84. else if (strcmp(devpath, "/dev/ttyUSB1") == 0)
  85. strcpy(gpio, "gpio114");
  86. else if (strcmp(devpath, "/dev/ttyUSB2") == 0)
  87. strcpy(gpio, "gpio115");
  88. else if (strcmp(devpath, "/dev/ttyUSB3") == 0)
  89. strcpy(gpio, "gpio116");
  90. else if (strcmp(devpath, "/dev/ttyAMA0") == 0)
  91. strcpy(gpio, "gpio25");
  92. else
  93. return;
  94. sprintf(buf, "/sys/class/gpio/%s/value", gpio);
  95. fd = open(buf, O_WRONLY);
  96. if (write(fd, "0", 1) != 1)
  97. applog(LOG_DEBUG, "%s: %s %s", alchemist_drv.dname, "GPIO write error", devpath);
  98. cgsleep_ms(100);
  99. if (write(fd, "1", 1) != 1)
  100. applog(LOG_DEBUG, "%s: %s %s", alchemist_drv.dname, "GPIO write error", devpath);
  101. close(fd);
  102. }
  103. static
  104. void alchemist_set_diag_mode(struct alchemist_chip * const chip, bool diag_enable)
  105. {
  106. if (diag_enable)
  107. chip->global_reg[1] |= 1;
  108. else
  109. chip->global_reg[1] &= ~1;
  110. }
  111. static
  112. bool alchemist_write_global_reg(const int fd, const struct alchemist_chip * const chip)
  113. {
  114. uint8_t buf[113];
  115. memset(&buf, 0, 102);
  116. memcpy(&buf[102], &chip->global_reg[0], 8);
  117. buf[110] = 0;
  118. buf[111] = 0xff;
  119. buf[112] = chip->chipid;
  120. //char output[(sizeof(chip->global_reg) * 2) + 1];
  121. //bin2hex(output, chip->global_reg, sizeof(chip->global_reg));
  122. //applog(LOG_DEBUG, "GLOBAL REG %s", output);
  123. if (write(fd, buf, sizeof(buf)) != sizeof(buf))
  124. return false;
  125. return true;
  126. }
  127. static
  128. bool alchemist_write_cluster_reg(const int fd, const struct alchemist_chip * const chip, const uint16_t cores_active, const uint32_t offset, const uint8_t clstid)
  129. {
  130. uint8_t buf[113];
  131. memset(&buf, 0, 104);
  132. pk_u16be(buf, 104, cores_active);
  133. pk_u32be(buf, 106, offset);
  134. buf[110] = clstid;
  135. buf[111] = 0xfe;
  136. buf[112] = chip->chipid;
  137. //applog(LOG_DEBUG, " %u: %u: %u : %u", buf[106], buf[107], buf[108], buf[109]);
  138. if (write(fd, buf, sizeof(buf)) != sizeof(buf))
  139. return false;
  140. return true;
  141. }
  142. static
  143. bool alchemist_init_pll(const int fd, struct alchemist_chip * const chip)
  144. {
  145. unsigned freq = chip->freq;
  146. uint8_t divider = (freq - 16)/16;
  147. divider <<= 1;
  148. uint8_t bytes1 = 0x60 | ((divider & 0xf0) >> 4);
  149. uint8_t bytes2 = 0x20 | ((divider & 0xf0) >> 4);
  150. uint8_t bytes3 = 0x00 | ((divider & 0x0f) << 4);
  151. pk_u16be(chip->global_reg, 2, 0x4000);
  152. chip->global_reg[1] |= 0xc;
  153. if (!alchemist_write_global_reg(fd, chip))
  154. return false;
  155. chip->global_reg[2] = bytes1;
  156. chip->global_reg[3] = bytes3;
  157. cgsleep_ms(20);
  158. if (!alchemist_write_global_reg(fd, chip))
  159. return false;
  160. chip->global_reg[2] = bytes2;
  161. chip->global_reg[1] &= ~8;
  162. cgsleep_ms(20);
  163. if (!alchemist_write_global_reg(fd, chip))
  164. return false;
  165. chip->global_reg[1] &= ~4;
  166. cgsleep_ms(20);
  167. if (!alchemist_write_global_reg(fd, chip))
  168. return false;
  169. return true;
  170. }
  171. static
  172. bool alchemist_send_golden(const int fd, const struct alchemist_chip * const chip, const bool diag, const void * const data, const void * const target_p)
  173. {
  174. uint8_t buf[113];
  175. const uint8_t * const target = target_p;
  176. memcpy(buf, data, 80);
  177. if (target && !target[0x1f])
  178. memcpy(&buf[80], target, 0x20);
  179. else
  180. {
  181. memset(&buf[80], 0xff, 0x1f);
  182. buf[111] = 0;
  183. }
  184. buf[112] = chip->chipid;
  185. if (diag)
  186. buf[112] |= 0x80;
  187. if (write(fd, buf, sizeof(buf)) != sizeof(buf))
  188. return false;
  189. return true;
  190. }
  191. static
  192. bool alchemist_send_work(const struct thr_info * const thr, struct work * const work)
  193. {
  194. struct cgpu_info *device = thr->cgpu;
  195. uint8_t buf[113];
  196. uint8_t cmd[113];
  197. const uint8_t * const target = work->target;
  198. unsigned char swpdata[80];
  199. buf[0] = 0xff;
  200. memset(&buf[1], 0, 0x18);
  201. memcpy(&buf[25], &target[24], 0x8);
  202. //pk_u64be(buf, 25, 0x0000feff01000000);
  203. swap32tobe(swpdata, work->data, 80/4);
  204. memcpy(&buf[33], swpdata, 80);
  205. for (int i = 0; i<113; i++) {
  206. cmd[i] = buf[112 - i];
  207. }
  208. //char output[(sizeof(cmd) * 2) + 1];
  209. //bin2hex(output, cmd, sizeof(cmd));
  210. //applog(LOG_DEBUG, "OUTPUT %s", output);
  211. if (write(device->device_fd, cmd, sizeof(cmd)) != sizeof(cmd))
  212. return false;
  213. work->blk.nonce = ALCHEMIST_MAX_NONCE;
  214. return true;
  215. }
  216. static
  217. bool alchemist_detect_one(const char * const devpath)
  218. {
  219. struct alchemist_chip *chips = NULL;
  220. const int fd = serial_open(devpath, 115200, 1, true);
  221. if (fd < 0)
  222. return_via_applog(err, , LOG_DEBUG, "%s: %s %s", alchemist_drv.dname, "Failed to open", devpath);
  223. applog(LOG_DEBUG, "%s: %s %s", alchemist_drv.dname, "Successfully opened", devpath);
  224. alchemist_reset_board(devpath);
  225. // Init chips, setup PLL, and scan for good cores
  226. chips = malloc(alchemist_max_chips * sizeof(*chips));
  227. struct alchemist_chip * const dummy_chip = &chips[0];
  228. alchemist_chip_init(dummy_chip, 0);
  229. // pick up any user-defined settings passed in via --set
  230. drv_set_defaults(&alchemist_drv, alchemist_set_device_funcs_probe, dummy_chip, devpath, detectone_meta_info.serial, 1);
  231. unsigned freq = dummy_chip->freq;
  232. unsigned total_cores = 0;
  233. {
  234. uint8_t buf[9];
  235. for (unsigned i = 0; i < alchemist_max_chips; ++i)
  236. {
  237. struct alchemist_chip * const chip = &chips[i];
  238. alchemist_chip_init(chip, i);
  239. chip->freq = freq;
  240. alchemist_set_diag_mode(chip, true);
  241. if (!alchemist_init_pll(fd, chip))
  242. return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", alchemist_drv.dname, "init PLL", devpath);
  243. if (!alchemist_send_golden(fd, chip, true, alchemist_g_head, NULL))
  244. return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", alchemist_drv.dname, "send scan job", devpath);
  245. while (serial_read(fd, buf, 9) == 9)
  246. {
  247. const uint8_t chipid = buf[8];
  248. if (chipid >= alchemist_max_chips)
  249. applog(LOG_DEBUG, "%s: Bad %s id (%u) during scan of %s chip %u", alchemist_drv.dname, "chip", chipid, devpath, i);
  250. const uint8_t clsid = buf[7];
  251. if (clsid >= alchemist_max_clusters_per_chip)
  252. applog(LOG_DEBUG, "%s: Bad %s id (%u) during scan of %s chip %u", alchemist_drv.dname, "cluster", clsid, devpath, i);
  253. const uint8_t coreid = buf[6];
  254. if (coreid >= alchemist_max_cores_per_cluster)
  255. applog(LOG_DEBUG, "%s: Bad %s id (%u) during scan of %s chip %u", alchemist_drv.dname, "core", coreid, devpath, i);
  256. if (buf[0] != 0xd9 || buf[1] != 0xeb || buf[2] != 0x86 || buf[3] != 0x63) {
  257. //chips[i].chip_good[clsid][coreid] = false;
  258. applog(LOG_DEBUG, "%s: Bad %s at core (%u) during scan of %s chip %u cluster %u", alchemist_drv.dname, "nonce", coreid, devpath, i, clsid);
  259. } else {
  260. ++total_cores;
  261. chips[i].chip_mask[clsid] |= (1 << coreid);
  262. }
  263. }
  264. }
  265. }
  266. applog(LOG_DEBUG, "%s: Identified %d cores on %s", alchemist_drv.dname, total_cores, devpath);
  267. if (!total_cores)
  268. goto err;
  269. alchemist_reset_board(devpath);
  270. // config nonce ranges per cluster based on core responses
  271. unsigned mutiple = ALCHEMIST_MAX_NONCE / total_cores;
  272. uint32_t n_offset = 0x00000000;
  273. for (unsigned i = 0; i < alchemist_max_chips; ++i)
  274. {
  275. struct alchemist_chip * const chip = &chips[i];
  276. chips[i].active_cores = total_cores;
  277. alchemist_set_diag_mode(chip, false);
  278. if (!alchemist_init_pll(fd, chip))
  279. return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", alchemist_drv.dname, "init PLL", devpath);
  280. cgsleep_ms(10);
  281. for (unsigned x = 0; x < alchemist_max_clusters_per_chip; ++x) {
  282. unsigned gc = 0;
  283. uint16_t core_mask = chips[i].chip_mask[x];
  284. chips[i].clst_offset[x] = n_offset;
  285. //applog(LOG_DEBUG, "OFFSET %u CHIP %u CLUSTER %u", n_offset, i, x);
  286. if (!alchemist_write_cluster_reg(fd, chip, core_mask, n_offset, x))
  287. return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", alchemist_drv.dname, "send config register", devpath);
  288. for (unsigned z = 0; z < 15; ++z) {
  289. if (core_mask & 0x0001)
  290. gc += 1;
  291. core_mask >>= 1;
  292. }
  293. n_offset += mutiple * gc;
  294. }
  295. }
  296. if (serial_claim_v(devpath, &alchemist_drv))
  297. goto err;
  298. //serial_close(fd);
  299. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  300. *cgpu = (struct cgpu_info){
  301. .drv = &alchemist_drv,
  302. .device_path = strdup(devpath),
  303. .deven = DEV_ENABLED,
  304. .procs = 1,
  305. .threads = 1,
  306. .device_data = chips,
  307. };
  308. // NOTE: Xcode's clang has a bug where it cannot find fields inside anonymous unions (more details in fpgautils)
  309. cgpu->device_fd = fd;
  310. return add_cgpu(cgpu);
  311. err:
  312. if (fd >= 0)
  313. serial_close(fd);
  314. free(chips);
  315. return false;
  316. }
  317. /*
  318. * scanhash mining loop
  319. */
  320. static
  321. void alchemist_submit_nonce(struct thr_info * const thr, const uint8_t buf[9], struct work * const work)
  322. {
  323. struct cgpu_info *device = thr->cgpu;
  324. struct alchemist_chip *chips = device->device_data;
  325. uint32_t nonce = *(uint32_t *)buf;
  326. nonce = bswap_32(nonce);
  327. submit_nonce(thr, work, nonce);
  328. // hashrate calc
  329. const uint8_t chipid = buf[8];
  330. const uint8_t clstid = buf[7];
  331. uint32_t range = chips[chipid].clst_offset[clstid];
  332. uint32_t mutiple = ALCHEMIST_MAX_NONCE / chips[chipid].active_cores;
  333. double diff_mutiple = .5/work->work_difficulty;
  334. for (unsigned x = 0; x < alchemist_max_cores_per_cluster; ++x) {
  335. if (nonce > range && nonce < (range + mutiple)) {
  336. uint64_t hashes = (nonce - range) * chips[chipid].active_cores * diff_mutiple;
  337. if (hashes > ALCHEMIST_MAX_NONCE)
  338. hashes = 1;
  339. hashes_done2(thr, hashes, NULL);
  340. }
  341. range += mutiple;
  342. }
  343. }
  344. // send work to the device
  345. static
  346. int64_t alchemist_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  347. {
  348. struct cgpu_info *device = thr->cgpu;
  349. int fd = device->device_fd;
  350. struct alchemist_chip *chips = device->device_data;
  351. struct timeval start_tv, nonce_range_tv;
  352. // amount of time it takes this device to scan a nonce range:
  353. uint32_t nonce_full_range_sec = ALCHEMIST_HASH_SPEED * 352.0 / ALCHEMIST_DEFAULT_FREQUENCY * 1728.0 / chips[0].active_cores;
  354. // timer to break out of scanning should we close in on an entire nonce range
  355. // should break out before the range is scanned, so we are doing 95% of the range
  356. uint64_t nonce_near_range_usec = (nonce_full_range_sec * 1000000. * 0.95);
  357. timer_set_delay_from_now(&nonce_range_tv, nonce_near_range_usec);
  358. // start the job
  359. timer_set_now(&start_tv);
  360. if (!alchemist_send_work(thr, work)) {
  361. applog(LOG_DEBUG, "Failed to start job");
  362. dev_error(device, REASON_DEV_COMMS_ERROR);
  363. }
  364. uint8_t buf[9];
  365. int read = 0;
  366. bool range_nearly_scanned = false;
  367. while (!thr->work_restart // true when new work is available (miner.c)
  368. && ((read = serial_read(fd, buf, 9)) >= 0) // only check for failure - allow 0 bytes
  369. && !(range_nearly_scanned = timer_passed(&nonce_range_tv, NULL))) // true when we've nearly scanned a nonce range
  370. {
  371. if (read == 0)
  372. continue;
  373. if (read == 9) {
  374. alchemist_submit_nonce(thr, buf, work);
  375. }
  376. else
  377. applog(LOG_ERR, "%"PRIpreprv": Unrecognized response", device->proc_repr);
  378. }
  379. if (read == -1)
  380. {
  381. applog(LOG_ERR, "%s: Failed to read result", device->dev_repr);
  382. dev_error(device, REASON_DEV_COMMS_ERROR);
  383. }
  384. return 0;
  385. }
  386. /*
  387. * setup & shutdown
  388. */
  389. static
  390. bool alchemist_lowl_probe(const struct lowlevel_device_info * const info)
  391. {
  392. return vcom_lowl_probe_wrapper(info, alchemist_detect_one);
  393. }
  394. static
  395. void alchemist_thread_shutdown(struct thr_info *thr)
  396. {
  397. struct cgpu_info *device = thr->cgpu;
  398. alchemist_reset_board(device->device_path);
  399. serial_close(device->device_fd);
  400. }
  401. /*
  402. * specify settings / options via RPC or command line
  403. */
  404. // support for --set
  405. // must be set before probing the device
  406. // for setting clock and chips during probe / detect
  407. static
  408. const char *alchemist_set_clock(struct cgpu_info * const device, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  409. {
  410. struct alchemist_chip * const chip = device->device_data;
  411. int val = atoi(setting);
  412. if (val < ALCHEMIST_MIN_CLOCK || val > ALCHEMIST_MAX_CLOCK || (val%16)) {
  413. sprintf(replybuf, "invalid clock: '%s' valid range %d-%d and a mutiple of 16",
  414. setting, ALCHEMIST_MIN_CLOCK, ALCHEMIST_MAX_CLOCK);
  415. return replybuf;
  416. } else
  417. chip->freq = val;
  418. return NULL;
  419. }
  420. static
  421. const struct bfg_set_device_definition alchemist_set_device_funcs_probe[] = {
  422. { "clock", alchemist_set_clock, NULL },
  423. { NULL },
  424. };
  425. struct device_drv alchemist_drv = {
  426. .dname = "alchemist",
  427. .name = "ALC",
  428. .drv_min_nonce_diff = common_scrypt_min_nonce_diff,
  429. // detect device
  430. .lowl_probe = alchemist_lowl_probe,
  431. // specify mining type - scanhash
  432. .minerloop = minerloop_scanhash,
  433. // scanhash mining hooks
  434. .scanhash = alchemist_scanhash,
  435. // teardown device
  436. .thread_shutdown = alchemist_thread_shutdown,
  437. };