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 <stdbool.h>
  26. #include "deviceapi.h"
  27. #include "driver-bitfury.h"
  28. #include "libbitfury.h"
  29. #include "lowl-spi.h"
  30. #include "tm_i2c.h"
  31. BFG_REGISTER_DRIVER(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. .set_device_funcs = bitfury_set_device_funcs,
  104. .procs = chip_n,
  105. .device_data = devicelist,
  106. .cutofftemp = 50,
  107. };
  108. add_cgpu_slave(cgpu, prev_cgpu);
  109. proc_count += chip_n;
  110. if (!proc1)
  111. proc1 = cgpu;
  112. prev_cgpu = cgpu;
  113. }
  114. else
  115. free(port);
  116. }
  117. }
  118. if (proc1)
  119. proc1->threads = 1;
  120. return proc_count;
  121. }
  122. static void metabank_detect(void)
  123. {
  124. noserial_detect_manual(&metabank_drv, metabank_autodetect);
  125. }
  126. static
  127. bool metabank_init(struct thr_info *thr)
  128. {
  129. struct bitfury_device **devicelist;
  130. struct cgpu_info *proc;
  131. struct bitfury_device *bitfury;
  132. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  133. {
  134. devicelist = proc->device_data;
  135. bitfury = devicelist[proc->proc_id];
  136. proc->device_data = bitfury;
  137. bitfury->spi->cgpu = proc;
  138. bitfury_init_chip(proc);
  139. bitfury->osc6_bits = 53;
  140. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  141. bitfury_init_freq_stat(&bitfury->chip_stat, 52, 56);
  142. if (proc->proc_id == proc->procs - 1)
  143. free(devicelist);
  144. }
  145. timer_set_now(&thr->tv_poll);
  146. return true;
  147. }
  148. static void metabank_shutdown(struct thr_info *thr)
  149. {
  150. bitfury_shutdown(thr);
  151. tm_i2c_close();
  152. }
  153. static bool metabank_get_stats(struct cgpu_info *cgpu)
  154. {
  155. struct bitfury_device * const bitfury = cgpu->device_data;
  156. float t;
  157. t = tm_i2c_gettemp(bitfury->slot) * 0.1;
  158. if (t < -27) //Sometimes tm_i2c_gettemp() returns strange result, ignoring it.
  159. return false;
  160. cgpu->temp = t;
  161. return true;
  162. }
  163. static struct api_data *metabank_api_extra_device_detail(struct cgpu_info *cgpu)
  164. {
  165. struct api_data *root = NULL;
  166. struct bitfury_device * const bitfury = cgpu->device_data;
  167. root = bitfury_api_device_detail(cgpu);
  168. root = api_add_uint(root, "Slot", &(bitfury->slot), false);
  169. return root;
  170. }
  171. static struct api_data *metabank_api_extra_device_status(struct cgpu_info *cgpu)
  172. {
  173. struct api_data *root = NULL;
  174. float vc0, vc1;
  175. struct bitfury_device * const bitfury = cgpu->device_data;
  176. root = bitfury_api_device_status(cgpu);
  177. vc0 = tm_i2c_getcore0(bitfury->slot);
  178. vc1 = tm_i2c_getcore1(bitfury->slot);
  179. root = api_add_volts(root, "Slot VC0", &vc0, true);
  180. root = api_add_volts(root, "Slot VC1", &vc1, true);
  181. return root;
  182. }
  183. struct device_drv metabank_drv = {
  184. .dname = "metabank",
  185. .name = "MBF",
  186. .drv_detect = metabank_detect,
  187. .thread_init = metabank_init,
  188. .thread_enable = bitfury_enable,
  189. .thread_disable = bitfury_disable,
  190. .minerloop = minerloop_async,
  191. .job_prepare = bitfury_job_prepare,
  192. .job_start = bitfury_noop_job_start,
  193. .poll = bitfury_do_io,
  194. .job_process_results = bitfury_job_process_results,
  195. .thread_shutdown = metabank_shutdown,
  196. .get_api_extra_device_detail = metabank_api_extra_device_detail,
  197. .get_api_extra_device_status = metabank_api_extra_device_status,
  198. .get_stats = metabank_get_stats,
  199. #ifdef HAVE_CURSES
  200. .proc_wlogprint_status = bitfury_wlogprint_status,
  201. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  202. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  203. #endif
  204. };