driver-metabank.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "tm_i2c.h"
  29. struct device_drv metabank_drv;
  30. static
  31. bool metabank_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. tm_i2c_set_oe(bitfury->slot);
  36. const bool rv = sys_spi_txrx(port);
  37. tm_i2c_clear_oe(bitfury->slot);
  38. return rv;
  39. }
  40. static
  41. struct bitfury_device **metabank_detect_chips(int *out_count) {
  42. struct bitfury_device **devicelist, *bitfury;
  43. struct spi_port *port;
  44. int n = 0;
  45. int i, j;
  46. bool slot_on[32];
  47. struct timespec t1, t2;
  48. struct bitfury_device dummy_bitfury;
  49. struct cgpu_info dummy_cgpu;
  50. int max_devices = 100;
  51. if (tm_i2c_init() < 0) {
  52. applog(LOG_DEBUG, "%s: I2C init error", metabank_drv.dname);
  53. *out_count = 0;
  54. return NULL;
  55. }
  56. devicelist = malloc(max_devices * sizeof(*devicelist));
  57. dummy_cgpu.device_data = &dummy_bitfury;
  58. for (i = 0; i < 32; i++) {
  59. slot_on[i] = 0;
  60. }
  61. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
  62. for (i = 0; i < 32; i++) {
  63. int slot_detected = tm_i2c_detect(i) != -1;
  64. slot_on[i] = slot_detected;
  65. tm_i2c_clear_oe(i);
  66. cgsleep_ms(1);
  67. }
  68. for (i = 0; i < 32; i++) {
  69. if (slot_on[i]) {
  70. int chip_n;
  71. port = malloc(sizeof(*port));
  72. *port = *sys_spi;
  73. port->cgpu = &dummy_cgpu;
  74. port->txrx = metabank_spi_txrx;
  75. dummy_bitfury.slot = i;
  76. chip_n = libbitfury_detectChips1(port);
  77. if (chip_n)
  78. {
  79. applog(LOG_WARNING, "BITFURY slot %d: %d chips detected", i, chip_n);
  80. for (j = 0; j < chip_n; ++j)
  81. {
  82. if (unlikely(n >= max_devices))
  83. {
  84. max_devices *= 2;
  85. devicelist = realloc(devicelist, max_devices * sizeof(*devicelist));
  86. }
  87. devicelist[n] = bitfury = malloc(sizeof(*bitfury));
  88. *bitfury = (struct bitfury_device){
  89. .spi = port,
  90. .slot = i,
  91. .fasync = j,
  92. };
  93. n++;
  94. }
  95. }
  96. else
  97. free(port);
  98. }
  99. }
  100. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);
  101. *out_count = n;
  102. return devicelist;
  103. }
  104. static
  105. int metabank_autodetect()
  106. {
  107. RUNONCE(0);
  108. int chip_n;
  109. struct cgpu_info *bitfury_info;
  110. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  111. bitfury_info->drv = &metabank_drv;
  112. bitfury_info->threads = 1;
  113. applog(LOG_INFO, "INFO: bitfury_detect");
  114. spi_init();
  115. if (!sys_spi)
  116. return 0;
  117. bitfury_info->device_data = metabank_detect_chips(&chip_n);
  118. if (!chip_n) {
  119. applog(LOG_WARNING, "No Bitfury chips detected!");
  120. return 0;
  121. } else {
  122. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  123. }
  124. bitfury_info->procs = chip_n;
  125. add_cgpu(bitfury_info);
  126. return 1;
  127. }
  128. static void metabank_detect(void)
  129. {
  130. noserial_detect_manual(&metabank_drv, metabank_autodetect);
  131. }
  132. extern bool bitfury_prepare(struct thr_info *);
  133. static
  134. bool metabank_init(struct thr_info *thr)
  135. {
  136. struct bitfury_device **devicelist = thr->cgpu->device_data;
  137. struct cgpu_info *proc;
  138. struct bitfury_device *bitfury;
  139. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  140. {
  141. bitfury = devicelist[proc->proc_id];
  142. proc->device_data = bitfury;
  143. bitfury->spi->cgpu = proc;
  144. }
  145. free(devicelist);
  146. return true;
  147. }
  148. extern int64_t bitfury_scanHash(struct thr_info *);
  149. extern void bitfury_shutdown(struct thr_info *);
  150. static void metabank_shutdown(struct thr_info *thr)
  151. {
  152. bitfury_shutdown(thr);
  153. tm_i2c_close();
  154. }
  155. struct device_drv metabank_drv = {
  156. .dname = "metabank",
  157. .name = "MBF",
  158. .drv_detect = metabank_detect,
  159. .minerloop = hash_queued_work,
  160. .thread_prepare = bitfury_prepare,
  161. .thread_init = metabank_init,
  162. .scanwork = bitfury_scanHash,
  163. .thread_shutdown = metabank_shutdown,
  164. };