driver-metabank.c 6.2 KB

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