driver-bfsb.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2013 bitfury
  3. * Copyright 2013 legkodymov
  4. * Copyright 2013 Luke Dashjr
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "config.h"
  25. #include "deviceapi.h"
  26. #include "libbitfury.h"
  27. #include "spidevc.h"
  28. struct device_drv bfsb_drv;
  29. static
  30. bool bfsb_spi_txrx(struct spi_port *port)
  31. {
  32. struct cgpu_info * const proc = port->cgpu;
  33. struct bitfury_device * const bitfury = proc->device_data;
  34. spi_bfsb_select_bank(bitfury->slot);
  35. const bool rv = sys_spi_txrx(port);
  36. return rv;
  37. }
  38. static
  39. struct bitfury_device **bfsb_detect_chips(int *out_count) {
  40. struct bitfury_device **devicelist, *bitfury;
  41. struct spi_port *port;
  42. int n = 0;
  43. int i, j;
  44. bool slot_on[32];
  45. struct timespec t1, t2;
  46. struct bitfury_device dummy_bitfury;
  47. struct cgpu_info dummy_cgpu;
  48. int max_devices = 100;
  49. devicelist = malloc(max_devices * sizeof(*devicelist));
  50. dummy_cgpu.device_data = &dummy_bitfury;
  51. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
  52. for (i = 0; i < 4; i++) {
  53. slot_on[i] = 1;
  54. }
  55. for (i = 0; i < 32; i++) {
  56. if (slot_on[i]) {
  57. int chip_n;
  58. port = malloc(sizeof(*port));
  59. *port = *sys_spi;
  60. port->cgpu = &dummy_cgpu;
  61. port->txrx = bfsb_spi_txrx;
  62. dummy_bitfury.slot = i;
  63. chip_n = libbitfury_detectChips1(port);
  64. if (chip_n)
  65. {
  66. applog(LOG_WARNING, "BITFURY slot %d: %d chips detected", i, chip_n);
  67. for (j = 0; j < chip_n; ++j)
  68. {
  69. if (unlikely(n >= max_devices))
  70. {
  71. max_devices *= 2;
  72. devicelist = realloc(devicelist, max_devices * sizeof(*devicelist));
  73. }
  74. devicelist[n] = bitfury = malloc(sizeof(*bitfury));
  75. *bitfury = (struct bitfury_device){
  76. .spi = port,
  77. .slot = i,
  78. .fasync = j,
  79. };
  80. n++;
  81. }
  82. }
  83. else
  84. free(port);
  85. }
  86. }
  87. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);
  88. *out_count = n;
  89. return devicelist;
  90. }
  91. static
  92. int bfsb_autodetect()
  93. {
  94. RUNONCE(0);
  95. int chip_n;
  96. struct cgpu_info *bitfury_info;
  97. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  98. bitfury_info->drv = &bfsb_drv;
  99. bitfury_info->threads = 1;
  100. applog(LOG_INFO, "INFO: bitfury_detect");
  101. spi_init();
  102. if (!sys_spi)
  103. return 0;
  104. bitfury_info->device_data = bfsb_detect_chips(&chip_n);
  105. if (!chip_n) {
  106. applog(LOG_WARNING, "No Bitfury chips detected!");
  107. return 0;
  108. } else {
  109. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  110. }
  111. bitfury_info->procs = chip_n;
  112. add_cgpu(bitfury_info);
  113. return 1;
  114. }
  115. static void bfsb_detect(void)
  116. {
  117. noserial_detect_manual(&bfsb_drv, bfsb_autodetect);
  118. }
  119. extern bool bitfury_prepare(struct thr_info *);
  120. static
  121. bool bfsb_init(struct thr_info *thr)
  122. {
  123. struct bitfury_device **devicelist = thr->cgpu->device_data;
  124. struct cgpu_info *proc;
  125. struct bitfury_device *bitfury;
  126. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  127. {
  128. bitfury = devicelist[proc->proc_id];
  129. proc->device_data = bitfury;
  130. bitfury->spi->cgpu = proc;
  131. }
  132. free(devicelist);
  133. return true;
  134. }
  135. extern int64_t bitfury_scanHash(struct thr_info *);
  136. extern void bitfury_shutdown(struct thr_info *);
  137. static void bfsb_shutdown(struct thr_info *thr)
  138. {
  139. bitfury_shutdown(thr);
  140. spi_bfsb_select_bank(-1);
  141. }
  142. struct device_drv bfsb_drv = {
  143. .dname = "bitfurystrikesback",
  144. .name = "BSB",
  145. .drv_detect = bfsb_detect,
  146. .minerloop = hash_queued_work,
  147. .thread_prepare = bitfury_prepare,
  148. .thread_init = bfsb_init,
  149. .scanwork = bitfury_scanHash,
  150. .thread_shutdown = bfsb_shutdown,
  151. };