driver-metabank.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. int metabank_autodetect()
  43. {
  44. RUNONCE(0);
  45. struct cgpu_info *cgpu = NULL, *proc1 = NULL, *prev_cgpu = NULL;
  46. struct bitfury_device **devicelist, *bitfury;
  47. struct spi_port *port;
  48. int i, j;
  49. int proc_count = 0;
  50. bool slot_on[32];
  51. struct bitfury_device dummy_bitfury;
  52. struct cgpu_info dummy_cgpu;
  53. applog(LOG_INFO, "INFO: bitfury_detect");
  54. spi_init();
  55. if (!sys_spi)
  56. return 0;
  57. if (tm_i2c_init() < 0) {
  58. applog(LOG_DEBUG, "%s: I2C init error", metabank_drv.dname);
  59. return 0;
  60. }
  61. dummy_cgpu.device_data = &dummy_bitfury;
  62. for (i = 0; i < 32; i++) {
  63. slot_on[i] = 0;
  64. }
  65. for (i = 0; i < 32; i++) {
  66. int slot_detected = tm_i2c_detect(i) != -1;
  67. slot_on[i] = slot_detected;
  68. tm_i2c_clear_oe(i);
  69. cgsleep_ms(1);
  70. }
  71. for (i = 0; i < 32; i++) {
  72. if (slot_on[i]) {
  73. int chip_n;
  74. port = malloc(sizeof(*port));
  75. *port = *sys_spi;
  76. port->cgpu = &dummy_cgpu;
  77. port->txrx = metabank_spi_txrx;
  78. dummy_bitfury.slot = i;
  79. chip_n = libbitfury_detectChips1(port);
  80. if (chip_n)
  81. {
  82. applog(LOG_WARNING, "BITFURY slot %d: %d chips detected", i, chip_n);
  83. devicelist = malloc(sizeof(*devicelist) * chip_n);
  84. for (j = 0; j < chip_n; ++j)
  85. {
  86. devicelist[j] = bitfury = malloc(sizeof(*bitfury));
  87. *bitfury = (struct bitfury_device){
  88. .spi = port,
  89. .slot = i,
  90. .fasync = j,
  91. };
  92. }
  93. cgpu = malloc(sizeof(*cgpu));
  94. *cgpu = (struct cgpu_info){
  95. .drv = &metabank_drv,
  96. .procs = chip_n,
  97. .device_data = devicelist,
  98. };
  99. add_cgpu_slave(cgpu, prev_cgpu);
  100. proc_count += chip_n;
  101. if (!proc1)
  102. proc1 = cgpu;
  103. prev_cgpu = cgpu;
  104. }
  105. else
  106. free(port);
  107. }
  108. }
  109. if (proc1)
  110. proc1->threads = 1;
  111. return proc_count;
  112. }
  113. static void metabank_detect(void)
  114. {
  115. noserial_detect_manual(&metabank_drv, metabank_autodetect);
  116. }
  117. static
  118. bool metabank_init(struct thr_info *thr)
  119. {
  120. struct bitfury_device **devicelist;
  121. struct cgpu_info *proc, *next_proc;
  122. struct bitfury_device *bitfury;
  123. int devid;
  124. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  125. {
  126. devicelist = proc->device_data;
  127. bitfury = devicelist[proc->proc_id];
  128. proc->device_data = bitfury;
  129. bitfury->spi->cgpu = proc;
  130. bitfury_init_oldbuf(proc);
  131. bitfury->osc6_bits = 54;
  132. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  133. if (proc->proc_id == proc->procs - 1)
  134. free(devicelist);
  135. }
  136. timer_set_now(&thr->tv_poll);
  137. return true;
  138. }
  139. static void metabank_shutdown(struct thr_info *thr)
  140. {
  141. bitfury_shutdown(thr);
  142. tm_i2c_close();
  143. }
  144. static bool metabank_get_stats(struct cgpu_info *cgpu)
  145. {
  146. struct bitfury_device * const bitfury = cgpu->device_data;
  147. float t;
  148. t = tm_i2c_gettemp(bitfury->slot) * 0.1;
  149. if (t < -27) //Sometimes tm_i2c_gettemp() returns strange result, ignoring it.
  150. return false;
  151. cgpu->temp = t;
  152. return true;
  153. }
  154. static struct api_data *metabank_api_extra_device_status(struct cgpu_info *cgpu)
  155. {
  156. struct api_data *root = NULL;
  157. float vc0, vc1;
  158. struct bitfury_device * const bitfury = cgpu->device_data;
  159. root = bitfury_api_device_status(cgpu);
  160. root = api_add_uint(root, "Slot", &(bitfury->slot), false);
  161. vc0 = tm_i2c_getcore0(bitfury->slot);
  162. vc1 = tm_i2c_getcore1(bitfury->slot);
  163. root = api_add_volts(root, "Slot VC0", &vc0, true);
  164. root = api_add_volts(root, "Slot VC1", &vc1, true);
  165. return root;
  166. }
  167. struct device_drv metabank_drv = {
  168. .dname = "metabank",
  169. .name = "MBF",
  170. .drv_detect = metabank_detect,
  171. .thread_init = metabank_init,
  172. #if 0
  173. .minerloop = hash_queued_work,
  174. .thread_prepare = bitfury_prepare,
  175. .scanwork = bitfury_scanHash,
  176. #endif
  177. .minerloop = minerloop_async,
  178. .job_prepare = bitfury_job_prepare,
  179. .job_start = bitfury_noop_job_start,
  180. .poll = bitfury_do_io,
  181. .job_process_results = bitfury_job_process_results,
  182. .thread_shutdown = metabank_shutdown,
  183. .get_api_extra_device_status = metabank_api_extra_device_status,
  184. .get_stats = metabank_get_stats,
  185. };