driver-metabank.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. if (tm_i2c_init() < 0) {
  51. applog(LOG_DEBUG, "%s: I2C init error", metabank_drv.dname);
  52. *out_count = 0;
  53. return NULL;
  54. }
  55. devicelist = malloc(100 * sizeof(*devicelist));
  56. dummy_cgpu.device_data = &dummy_bitfury;
  57. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
  58. for (i = 0; i < 32; i++) {
  59. int slot_detected = tm_i2c_detect(i) != -1;
  60. slot_on[i] = slot_detected;
  61. tm_i2c_clear_oe(i);
  62. cgsleep_ms(1);
  63. }
  64. for (i = 0; i < 32; i++) {
  65. if (slot_on[i]) {
  66. int chip_n;
  67. port = malloc(sizeof(*port));
  68. *port = *sys_spi;
  69. port->cgpu = &dummy_cgpu;
  70. port->txrx = metabank_spi_txrx;
  71. dummy_bitfury.slot = i;
  72. chip_n = libbitfury_detectChips1(port);
  73. if (chip_n)
  74. {
  75. applog(LOG_WARNING, "BITFURY slot %d: %d chips detected", i, chip_n);
  76. for (j = 0; j < chip_n; ++j)
  77. {
  78. devicelist[n] = bitfury = malloc(sizeof(*bitfury));
  79. bitfury->spi = port;
  80. bitfury->slot = i;
  81. bitfury->fasync = j;
  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 metabank_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 = &metabank_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 = metabank_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 metabank_detect(void)
  118. {
  119. noserial_detect_manual(&metabank_drv, metabank_autodetect);
  120. }
  121. extern bool bitfury_prepare(struct thr_info *);
  122. static
  123. bool metabank_init(struct thr_info *thr)
  124. {
  125. struct bitfury_device **devicelist = thr->cgpu->device_data;
  126. struct cgpu_info *proc;
  127. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  128. proc->device_data = devicelist[proc->proc_id];
  129. free(devicelist);
  130. return true;
  131. }
  132. extern int64_t bitfury_scanHash(struct thr_info *);
  133. extern void bitfury_shutdown(struct thr_info *);
  134. static void metabank_shutdown(struct thr_info *thr)
  135. {
  136. bitfury_shutdown(thr);
  137. tm_i2c_close();
  138. }
  139. struct device_drv metabank_drv = {
  140. .dname = "metabank",
  141. .name = "MBF",
  142. .drv_detect = metabank_detect,
  143. .minerloop = hash_queued_work,
  144. .thread_prepare = bitfury_prepare,
  145. .thread_init = metabank_init,
  146. .scanwork = bitfury_scanHash,
  147. .thread_shutdown = metabank_shutdown,
  148. };