driver-metabank.c 4.8 KB

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