driver-metabank.c 6.1 KB

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