libbitfury.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Copyright 2014 Con Kolivas
  3. * Copyright 2013 Andrew Smith
  4. * Copyright 2013 bitfury
  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 "miner.h"
  12. #include "driver-bitfury.h"
  13. #include "libbitfury.h"
  14. #include "sha2.h"
  15. void ms3steps(uint32_t *p)
  16. {
  17. uint32_t a, b, c, d, e, f, g, h, new_e, new_a;
  18. int i;
  19. a = p[0];
  20. b = p[1];
  21. c = p[2];
  22. d = p[3];
  23. e = p[4];
  24. f = p[5];
  25. g = p[6];
  26. h = p[7];
  27. for (i = 0; i < 3; i++) {
  28. new_e = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) + d;
  29. new_a = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) +
  30. SHA256_F1(a) + MAJ(a,b,c);
  31. d = c;
  32. c = b;
  33. b = a;
  34. a = new_a;
  35. h = g;
  36. g = f;
  37. f = e;
  38. e = new_e;
  39. }
  40. p[15] = a;
  41. p[14] = b;
  42. p[13] = c;
  43. p[12] = d;
  44. p[11] = e;
  45. p[10] = f;
  46. p[9] = g;
  47. p[8] = h;
  48. }
  49. uint32_t decnonce(uint32_t in)
  50. {
  51. uint32_t out;
  52. /* First part load */
  53. out = (in & 0xFF) << 24;
  54. in >>= 8;
  55. /* Byte reversal */
  56. in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
  57. in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
  58. in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
  59. out |= (in >> 2) & 0x3FFFFF;
  60. /* Extraction */
  61. if (in & 1)
  62. out |= (1 << 23);
  63. if (in & 2)
  64. out |= (1 << 22);
  65. out -= 0x800004;
  66. return out;
  67. }
  68. /* Test vectors to calculate (using address-translated loads) */
  69. static unsigned int atrvec[] = {
  70. 0xb0e72d8e, 0x1dc5b862, 0xe9e7c4a6, 0x3050f1f5, 0x8a1a6b7e, 0x7ec384e8, 0x42c1c3fc, 0x8ed158a1, /* MIDSTATE */
  71. 0,0,0,0,0,0,0,0,
  72. 0x8a0bb7b7, 0x33af304f, 0x0b290c1a, 0xf0c4e61f, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
  73. };
  74. static bool atrvec_set;
  75. void bitfury_work_to_payload(struct bitfury_payload *p, struct work *work)
  76. {
  77. memcpy(p->midstate, work->midstate, 32);
  78. p->m7 = *(unsigned int *)(work->data + 64);
  79. p->ntime = *(unsigned int *)(work->data + 68);
  80. p->nbits = *(unsigned int *)(work->data + 72);
  81. applog(LOG_INFO, "INFO nonc: %08x bitfury_scanHash MS0: %08x, ", p->nnonce,
  82. ((unsigned int *)work->midstate)[0]);
  83. applog(LOG_INFO, "INFO merkle[7]: %08x, ntime: %08x, nbits: %08x", p->m7,
  84. p->ntime, p->nbits);
  85. }
  86. /* Configuration registers - control oscillators and such stuff. PROGRAMMED when
  87. * magic number matches, UNPROGRAMMED (default) otherwise */
  88. void spi_config_reg(struct bitfury_info *info, int cfgreg, int ena)
  89. {
  90. static const uint8_t enaconf[4] = { 0xc1, 0x6a, 0x59, 0xe3 };
  91. static const uint8_t disconf[4] = { 0, 0, 0, 0 };
  92. if (ena)
  93. spi_add_data(info, 0x7000 + cfgreg * 32, enaconf, 4);
  94. else
  95. spi_add_data(info, 0x7000 + cfgreg * 32, disconf, 4);
  96. }
  97. void spi_set_freq(struct bitfury_info *info)
  98. {
  99. uint64_t freq;
  100. const uint8_t *osc6 = (unsigned char *)&freq;
  101. freq = (1ULL << info->osc6_bits) - 1ULL;
  102. spi_add_data(info, 0x6000, osc6, 8); /* Program internal on-die slow oscillator frequency */
  103. }
  104. #define FIRST_BASE 61
  105. #define SECOND_BASE 4
  106. void spi_send_conf(struct bitfury_info *info)
  107. {
  108. const int8_t nfu_counters[16] = { 64, 64, SECOND_BASE, SECOND_BASE+4, SECOND_BASE+2,
  109. SECOND_BASE+2+16, SECOND_BASE, SECOND_BASE+1, (FIRST_BASE)%65, (FIRST_BASE+1)%65,
  110. (FIRST_BASE+3)%65, (FIRST_BASE+3+16)%65, (FIRST_BASE+4)%65, (FIRST_BASE+4+4)%65,
  111. (FIRST_BASE+3+3)%65, (FIRST_BASE+3+1+3)%65 };
  112. int i;
  113. for (i = 7; i <= 11; i++)
  114. spi_config_reg(info, i, 0);
  115. spi_config_reg(info, 6, 1); /* disable OUTSLK */
  116. spi_config_reg(info, 4, 1); /* Enable slow oscillator */
  117. for (i = 1; i <= 3; ++i)
  118. spi_config_reg(info, i, 0);
  119. /* Program counters correctly for rounds processing, here it should
  120. * start consuming power */
  121. spi_add_data(info, 0x0100, nfu_counters, 16);
  122. }
  123. void spi_send_init(struct bitfury_info *info)
  124. {
  125. /* Prepare internal buffers */
  126. /* PREPARE BUFFERS (INITIAL PROGRAMMING) */
  127. unsigned int w[16];
  128. if (!atrvec_set) {
  129. atrvec_set = true;
  130. ms3steps(atrvec);
  131. }
  132. memset(w, 0, sizeof(w));
  133. w[3] = 0xffffffff;
  134. w[4] = 0x80000000;
  135. w[15] = 0x00000280;
  136. spi_add_data(info, 0x1000, w, 16 * 4);
  137. spi_add_data(info, 0x1400, w, 8 * 4);
  138. memset(w, 0, sizeof(w));
  139. w[0] = 0x80000000;
  140. w[7] = 0x100;
  141. spi_add_data(info, 0x1900, w, 8 * 4); /* Prepare MS and W buffers! */
  142. spi_add_data(info, 0x3000, atrvec, 19 * 4);
  143. }
  144. void spi_clear_buf(struct bitfury_info *info)
  145. {
  146. info->spibufsz = 0;
  147. }
  148. void spi_add_buf(struct bitfury_info *info, const void *buf, const int sz)
  149. {
  150. if (unlikely(info->spibufsz + sz > SPIBUF_SIZE)) {
  151. applog(LOG_WARNING, "SPI bufsize overflow!");
  152. return;
  153. }
  154. memcpy(&info->spibuf[info->spibufsz], buf, sz);
  155. info->spibufsz += sz;
  156. }
  157. void spi_add_break(struct bitfury_info *info)
  158. {
  159. spi_add_buf(info, "\x4", 1);
  160. }
  161. void spi_add_fasync(struct bitfury_info *info, int n)
  162. {
  163. int i;
  164. for (i = 0; i < n; i++)
  165. spi_add_buf(info, "\x5", 1);
  166. }
  167. static void spi_add_buf_reverse(struct bitfury_info *info, const char *buf, const int sz)
  168. {
  169. int i;
  170. for (i = 0; i < sz; i++) { // Reverse bit order in each byte!
  171. unsigned char p = buf[i];
  172. p = ((p & 0xaa) >> 1) | ((p & 0x55) << 1);
  173. p = ((p & 0xcc) >> 2) | ((p & 0x33) << 2);
  174. p = ((p & 0xf0) >> 4) | ((p & 0x0f) << 4);
  175. info->spibuf[info->spibufsz + i] = p;
  176. }
  177. info->spibufsz += sz;
  178. }
  179. void spi_add_data(struct bitfury_info *info, uint16_t addr, const void *buf, int len)
  180. {
  181. unsigned char otmp[3];
  182. if (len < 4 || len > 128) {
  183. applog(LOG_WARNING, "Can't add SPI data size %d", len);
  184. return;
  185. }
  186. len /= 4; /* Strip */
  187. otmp[0] = (len - 1) | 0xE0;
  188. otmp[1] = (addr >> 8) & 0xFF;
  189. otmp[2] = addr & 0xFF;
  190. spi_add_buf(info, otmp, 3);
  191. len *= 4;
  192. spi_add_buf_reverse(info, buf, len);
  193. }
  194. // Bit-banging reset... Each 3 reset cycles reset first chip in chain
  195. bool spi_reset(struct cgpu_info *bitfury, struct bitfury_info *info)
  196. {
  197. struct mcp_settings *mcp = &info->mcp;
  198. int r;
  199. // SCK_OVRRIDE
  200. mcp->value.pin[NFU_PIN_SCK_OVR] = MCP2210_GPIO_PIN_HIGH;
  201. mcp->direction.pin[NFU_PIN_SCK_OVR] = MCP2210_GPIO_OUTPUT;
  202. mcp->designation.pin[NFU_PIN_SCK_OVR] = MCP2210_PIN_GPIO;
  203. if (!mcp2210_set_gpio_settings(bitfury, mcp))
  204. return false;
  205. for (r = 0; r < 16; ++r) {
  206. char buf[1] = {0x81}; // will send this waveform: - _ _ _ _ _ _ -
  207. unsigned int length = 1;
  208. if (!mcp2210_spi_transfer(bitfury, &info->mcp, buf, &length))
  209. return false;
  210. }
  211. // Deactivate override
  212. mcp->direction.pin[NFU_PIN_SCK_OVR] = MCP2210_GPIO_INPUT;
  213. if (!mcp2210_set_gpio_settings(bitfury, mcp))
  214. return false;
  215. return true;
  216. }
  217. bool mcp_spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info)
  218. {
  219. unsigned int length, sendrcv;
  220. int offset = 0;
  221. length = info->spibufsz;
  222. applog(LOG_DEBUG, "%s %d: SPI sending %u bytes total", bitfury->drv->name,
  223. bitfury->device_id, length);
  224. while (length > MCP2210_TRANSFER_MAX) {
  225. sendrcv = MCP2210_TRANSFER_MAX;
  226. if (!mcp2210_spi_transfer(bitfury, &info->mcp, info->spibuf + offset, &sendrcv))
  227. return false;
  228. if (sendrcv != MCP2210_TRANSFER_MAX) {
  229. applog(LOG_DEBUG, "%s %d: Send/Receive size mismatch sent %d received %d",
  230. bitfury->drv->name, bitfury->device_id, MCP2210_TRANSFER_MAX, sendrcv);
  231. }
  232. length -= MCP2210_TRANSFER_MAX;
  233. offset += MCP2210_TRANSFER_MAX;
  234. }
  235. sendrcv = length;
  236. if (!mcp2210_spi_transfer(bitfury, &info->mcp, info->spibuf + offset, &sendrcv))
  237. return false;
  238. if (sendrcv != length) {
  239. applog(LOG_WARNING, "%s %d: Send/Receive size mismatch sent %d received %d",
  240. bitfury->drv->name, bitfury->device_id, length, sendrcv);
  241. return false;
  242. }
  243. return true;
  244. }
  245. #define READ_WRITE_BYTES_SPI0 0x31
  246. bool ftdi_spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info)
  247. {
  248. int err, amount, len;
  249. uint16_t length;
  250. char buf[1024];
  251. len = info->spibufsz;
  252. length = info->spibufsz - 1; //FTDI length is shifted by one 0x0000 = one byte
  253. buf[0] = READ_WRITE_BYTES_SPI0;
  254. buf[1] = length & 0x00FF;
  255. buf[2] = (length & 0xFF00) >> 8;
  256. memcpy(&buf[3], info->spibuf, info->spibufsz);
  257. info->spibufsz += 3;
  258. err = usb_write(bitfury, buf, info->spibufsz, &amount, C_BXM_SPITX);
  259. if (err || amount != (int)info->spibufsz) {
  260. applog(LOG_ERR, "%s %d: SPI TX error %d, sent %d of %d", bitfury->drv->name,
  261. bitfury->device_id, err, amount, info->spibufsz);
  262. return false;
  263. }
  264. info->spibufsz = len;
  265. /* We shouldn't even get a timeout error on reads in spi mode */
  266. err = usb_read(bitfury, info->spibuf, len, &amount, C_BXM_SPIRX);
  267. if (err || amount != len) {
  268. applog(LOG_ERR, "%s %d: SPI RX error %d, read %d of %d", bitfury->drv->name,
  269. bitfury->device_id, err, amount, info->spibufsz);
  270. return false;
  271. }
  272. amount = usb_buffer_size(bitfury);
  273. if (amount) {
  274. applog(LOG_ERR, "%s %d: SPI RX Extra read buffer size %d", bitfury->drv->name,
  275. bitfury->device_id, amount);
  276. usb_buffer_clear(bitfury);
  277. return false;
  278. }
  279. return true;
  280. }
  281. #define BT_OFFSETS 3
  282. bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32_t nonce)
  283. {
  284. const uint32_t bf_offsets[] = {-0x800000, 0, -0x400000};
  285. int i;
  286. for (i = 0; i < BT_OFFSETS; i++) {
  287. uint32_t noffset = nonce + bf_offsets[i];
  288. if (test_nonce(work, noffset)) {
  289. submit_tested_work(thr, work);
  290. return true;
  291. }
  292. }
  293. return false;
  294. }
  295. /* Currently really only supports 2 chips, so chip_n can only be 0 or 1 */
  296. bool libbitfury_sendHashData(struct thr_info *thr, struct cgpu_info *bitfury,
  297. struct bitfury_info *info, int chip_n)
  298. {
  299. unsigned newbuf[17];
  300. unsigned *oldbuf = &info->oldbuf[17 * chip_n];
  301. struct bitfury_payload *p = &info->payload[chip_n];
  302. unsigned int localvec[20];
  303. /* Programming next value */
  304. memcpy(localvec, p, 20 * 4);
  305. ms3steps(localvec);
  306. spi_clear_buf(info);
  307. spi_add_break(info);
  308. spi_add_fasync(info, chip_n);
  309. spi_add_data(info, 0x3000, (void*)localvec, 19 * 4);
  310. if (!info->spi_txrx(bitfury, info))
  311. return false;
  312. memcpy(newbuf, info->spibuf + 4 + chip_n, 17 * 4);
  313. info->job_switched[chip_n] = newbuf[16] != oldbuf[16];
  314. if (likely(info->second_run[chip_n])) {
  315. if (info->job_switched[chip_n]) {
  316. int i;
  317. for (i = 0; i < 16; i++) {
  318. if (oldbuf[i] != newbuf[i] && info->owork[chip_n]) {
  319. uint32_t nonce; //possible nonce
  320. nonce = decnonce(newbuf[i]);
  321. if (bitfury_checkresults(thr, info->owork[chip_n], nonce)) {
  322. info->submits[chip_n]++;
  323. info->nonces++;
  324. }
  325. }
  326. }
  327. memcpy(oldbuf, newbuf, 17 * 4);
  328. }
  329. } else
  330. info->second_run[chip_n] = true;
  331. cgsleep_ms(BITFURY_REFRESH_DELAY);
  332. return true;
  333. }