driver-bfsb.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #include "driver-bitfury.h"
  29. struct device_drv bfsb_drv;
  30. static
  31. bool bfsb_spi_txrx(struct spi_port *port)
  32. {
  33. struct cgpu_info * const proc = port->cgpu;
  34. struct bitfury_device * const bitfury = proc->device_data;
  35. spi_bfsb_select_bank(bitfury->slot);
  36. const bool rv = sys_spi_txrx(port);
  37. return rv;
  38. }
  39. static
  40. struct bitfury_device **bfsb_detect_chips(int *out_count) {
  41. struct bitfury_device **devicelist, *bitfury;
  42. struct spi_port *port;
  43. int n = 0;
  44. int i, j;
  45. bool slot_on[32];
  46. struct timespec t1, t2;
  47. struct bitfury_device dummy_bitfury;
  48. struct cgpu_info dummy_cgpu;
  49. int max_devices = 100;
  50. devicelist = malloc(max_devices * sizeof(*devicelist));
  51. dummy_cgpu.device_data = &dummy_bitfury;
  52. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
  53. for (i = 0; i < 4; i++) {
  54. slot_on[i] = 1;
  55. }
  56. for (i = 0; i < 32; i++) {
  57. if (slot_on[i]) {
  58. int chip_n;
  59. port = malloc(sizeof(*port));
  60. *port = *sys_spi;
  61. port->cgpu = &dummy_cgpu;
  62. port->txrx = bfsb_spi_txrx;
  63. port->speed = 625000;
  64. dummy_bitfury.slot = i;
  65. chip_n = libbitfury_detectChips1(port);
  66. if (chip_n)
  67. {
  68. applog(LOG_WARNING, "BITFURY slot %d: %d chips detected", i, chip_n);
  69. for (j = 0; j < chip_n; ++j)
  70. {
  71. if (unlikely(n >= max_devices))
  72. {
  73. max_devices *= 2;
  74. devicelist = realloc(devicelist, max_devices * sizeof(*devicelist));
  75. }
  76. devicelist[n] = bitfury = malloc(sizeof(*bitfury));
  77. *bitfury = (struct bitfury_device){
  78. .spi = port,
  79. .slot = i,
  80. .fasync = j,
  81. };
  82. n++;
  83. }
  84. }
  85. else
  86. free(port);
  87. }
  88. }
  89. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);
  90. *out_count = n;
  91. return devicelist;
  92. }
  93. static
  94. int bfsb_autodetect()
  95. {
  96. RUNONCE(0);
  97. int chip_n;
  98. struct cgpu_info *bitfury_info;
  99. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  100. bitfury_info->drv = &bfsb_drv;
  101. bitfury_info->threads = 1;
  102. applog(LOG_INFO, "INFO: bitfury_detect");
  103. spi_init();
  104. if (!sys_spi)
  105. return 0;
  106. bitfury_info->device_data = bfsb_detect_chips(&chip_n);
  107. if (!chip_n) {
  108. applog(LOG_WARNING, "No Bitfury chips detected!");
  109. return 0;
  110. } else {
  111. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  112. }
  113. bitfury_info->procs = chip_n;
  114. add_cgpu(bitfury_info);
  115. return 1;
  116. }
  117. static void bfsb_detect(void)
  118. {
  119. noserial_detect_manual(&bfsb_drv, bfsb_autodetect);
  120. }
  121. static
  122. bool bfsb_init(struct thr_info *thr)
  123. {
  124. struct bitfury_device **devicelist = thr->cgpu->device_data;
  125. struct cgpu_info *proc;
  126. struct bitfury_device *bitfury;
  127. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  128. {
  129. bitfury = devicelist[proc->proc_id];
  130. proc->device_data = bitfury;
  131. bitfury->spi->cgpu = proc;
  132. bitfury_init_oldbuf(proc);
  133. bitfury->osc6_bits = 54;
  134. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  135. }
  136. free(devicelist);
  137. return true;
  138. }
  139. extern void bitfury_shutdown(struct thr_info *);
  140. static void bfsb_shutdown(struct thr_info *thr)
  141. {
  142. bitfury_shutdown(thr);
  143. spi_bfsb_select_bank(-1);
  144. }
  145. struct device_drv bfsb_drv = {
  146. .dname = "bfsb",
  147. .name = "BSB",
  148. .drv_detect = bfsb_detect,
  149. .minerloop = minerloop_async,
  150. .job_prepare = bitfury_job_prepare,
  151. .thread_init = bfsb_init,
  152. .poll = bitfury_do_io,
  153. .job_start = bitfury_do_io,
  154. .job_process_results = bitfury_job_process_results,
  155. .thread_shutdown = bfsb_shutdown,
  156. };