driver-bitfury.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright 2013 bitfury
  3. * Copyright 2013 legkodymov
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "config.h"
  24. #include "miner.h"
  25. #include <unistd.h>
  26. #include <sha2.h>
  27. #include "fpgautils.h"
  28. #include "libbitfury.h"
  29. #include "util.h"
  30. #include "spidevc.h"
  31. #define GOLDEN_BACKLOG 5
  32. struct device_drv bitfury_drv;
  33. // Forward declarations
  34. static bool bitfury_prepare(struct thr_info *thr);
  35. static
  36. int bitfury_autodetect()
  37. {
  38. RUNONCE(0);
  39. int chip_n;
  40. struct cgpu_info *bitfury_info;
  41. applog(LOG_INFO, "INFO: bitfury_detect");
  42. spi_init();
  43. if (!sys_spi)
  44. return 0;
  45. chip_n = libbitfury_detectChips(sys_spi);
  46. if (!chip_n) {
  47. applog(LOG_WARNING, "No Bitfury chips detected!");
  48. return 0;
  49. } else {
  50. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  51. }
  52. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  53. bitfury_info->drv = &bitfury_drv;
  54. bitfury_info->threads = 1;
  55. bitfury_info->procs = chip_n;
  56. add_cgpu(bitfury_info);
  57. return 1;
  58. }
  59. static void bitfury_detect(void)
  60. {
  61. noserial_detect_manual(&bitfury_drv, bitfury_autodetect);
  62. }
  63. static
  64. bool bitfury_init(struct thr_info *thr)
  65. {
  66. struct cgpu_info *proc;
  67. struct bitfury_device *bitfury;
  68. int i = 0;
  69. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  70. {
  71. bitfury = proc->device_data = malloc(sizeof(struct bitfury_device));
  72. *bitfury = (struct bitfury_device){
  73. .spi = sys_spi,
  74. .chip = i++,
  75. };
  76. }
  77. return true;
  78. }
  79. int64_t bitfury_scanHash(struct thr_info *thr)
  80. {
  81. struct cgpu_info * const cgpu = thr->cgpu;
  82. struct cgpu_info *proc;
  83. struct thr_info *pthr;
  84. struct bitfury_device *bitfury;
  85. for (proc = cgpu; proc; proc = proc->next_proc)
  86. {
  87. pthr = proc->thr[0];
  88. bitfury = proc->device_data;
  89. if(!bitfury->work) {
  90. bitfury->work = get_queued(thr->cgpu);
  91. if (bitfury->work == NULL)
  92. return 0;
  93. work_to_payload(&bitfury->payload, bitfury->work);
  94. }
  95. payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  96. libbitfury_sendHashData1(bitfury, true);
  97. if (bitfury->job_switched) {
  98. int i,j;
  99. unsigned int * const res = bitfury->results;
  100. struct work * const owork = bitfury->owork;
  101. i = bitfury->results_n;
  102. for (j = i - 1; j >= 0; j--) {
  103. if (owork) {
  104. submit_nonce(pthr, owork, bswap_32(res[j]));
  105. }
  106. }
  107. if (owork)
  108. work_completed(cgpu, owork);
  109. bitfury->owork = bitfury->work;
  110. bitfury->work = NULL;
  111. hashes_done2(pthr, 0x100000000, NULL);
  112. }
  113. }
  114. return 0;
  115. }
  116. static bool bitfury_prepare(struct thr_info *thr)
  117. {
  118. struct cgpu_info *cgpu = thr->cgpu;
  119. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  120. applog(LOG_INFO, "INFO bitfury_prepare");
  121. return true;
  122. }
  123. static void bitfury_shutdown(struct thr_info *thr)
  124. {
  125. applog(LOG_INFO, "INFO bitfury_shutdown");
  126. }
  127. struct device_drv bitfury_drv = {
  128. .dname = "bitfury",
  129. .name = "BFY",
  130. .drv_detect = bitfury_detect,
  131. .thread_prepare = bitfury_prepare,
  132. .thread_init = bitfury_init,
  133. .scanwork = bitfury_scanHash,
  134. .thread_shutdown = bitfury_shutdown,
  135. .minerloop = hash_queued_work,
  136. };