driver-bfsb.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "libbitfury.h"
  28. #include "spidevc.h"
  29. #include "driver-bitfury.h"
  30. struct device_drv bfsb_drv;
  31. static
  32. bool bfsb_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. spi_bfsb_select_bank(bitfury->slot);
  37. const bool rv = sys_spi_txrx(port);
  38. return rv;
  39. }
  40. static
  41. int bfsb_autodetect()
  42. {
  43. RUNONCE(0);
  44. struct cgpu_info *cgpu = NULL, *proc1 = NULL, *prev_cgpu = NULL;
  45. int proc_count = 0;
  46. applog(LOG_INFO, "INFO: bitfury_detect");
  47. spi_init();
  48. if (!sys_spi)
  49. return 0;
  50. struct bitfury_device **devicelist, *bitfury;
  51. struct spi_port *port;
  52. int i, j;
  53. bool slot_on[32];
  54. struct timespec t1, t2;
  55. struct bitfury_device dummy_bitfury;
  56. struct cgpu_info dummy_cgpu;
  57. dummy_cgpu.device_data = &dummy_bitfury;
  58. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
  59. for (i = 0; i < 4; i++) {
  60. slot_on[i] = 1;
  61. }
  62. for (i = 0; i < 32; i++) {
  63. if (slot_on[i]) {
  64. int chip_n;
  65. port = malloc(sizeof(*port));
  66. *port = *sys_spi;
  67. port->cgpu = &dummy_cgpu;
  68. port->txrx = bfsb_spi_txrx;
  69. port->speed = 625000;
  70. dummy_bitfury.slot = i;
  71. chip_n = libbitfury_detectChips1(port);
  72. if (chip_n)
  73. {
  74. applog(LOG_WARNING, "BITFURY slot %d: %d chips detected", i, chip_n);
  75. devicelist = malloc(sizeof(*devicelist) * chip_n);
  76. for (j = 0; j < chip_n; ++j)
  77. {
  78. devicelist[j] = bitfury = malloc(sizeof(*bitfury));
  79. *bitfury = (struct bitfury_device){
  80. .spi = port,
  81. .slot = i,
  82. .fasync = j,
  83. };
  84. }
  85. cgpu = malloc(sizeof(*cgpu));
  86. *cgpu = (struct cgpu_info){
  87. .drv = &bfsb_drv,
  88. .procs = chip_n,
  89. .device_data = devicelist,
  90. };
  91. add_cgpu_slave(cgpu, prev_cgpu);
  92. proc_count += chip_n;
  93. if (!proc1)
  94. proc1 = cgpu;
  95. prev_cgpu = cgpu;
  96. }
  97. else
  98. free(port);
  99. }
  100. }
  101. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);
  102. if (proc1)
  103. proc1->threads = 1;
  104. return proc_count;
  105. }
  106. static void bfsb_detect(void)
  107. {
  108. noserial_detect_manual(&bfsb_drv, bfsb_autodetect);
  109. }
  110. static
  111. bool bfsb_init(struct thr_info *thr)
  112. {
  113. struct bitfury_device **devicelist;
  114. struct cgpu_info *proc;
  115. struct bitfury_device *bitfury;
  116. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  117. {
  118. devicelist = proc->device_data;
  119. bitfury = devicelist[proc->proc_id];
  120. proc->device_data = bitfury;
  121. bitfury->spi->cgpu = proc;
  122. bitfury_init_chip(proc);
  123. bitfury->osc6_bits = 53;
  124. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  125. bitfury_init_freq_stat(&bitfury->chip_stat, 52, 56);
  126. if (proc->proc_id == proc->procs - 1)
  127. free(devicelist);
  128. }
  129. timer_set_now(&thr->tv_poll);
  130. return true;
  131. }
  132. static
  133. void bfsb_disable(struct thr_info * const thr)
  134. {
  135. struct cgpu_info * const proc = thr->cgpu;
  136. struct bitfury_device * const bitfury = proc->device_data;
  137. applog(LOG_DEBUG, "%"PRIpreprv": Shutting down chip (disable)", proc->proc_repr);
  138. send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  139. }
  140. static
  141. void bfsb_enable(struct thr_info * const thr)
  142. {
  143. struct cgpu_info * const proc = thr->cgpu;
  144. struct bitfury_device * const bitfury = proc->device_data;
  145. applog(LOG_DEBUG, "%"PRIpreprv": Reinitialising chip (enable)", proc->proc_repr);
  146. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  147. bitfury_init_chip(proc);
  148. }
  149. extern void bitfury_shutdown(struct thr_info *);
  150. static void bfsb_shutdown(struct thr_info *thr)
  151. {
  152. bitfury_shutdown(thr);
  153. spi_bfsb_select_bank(-1);
  154. }
  155. static struct api_data *bfsb_api_device_detail(struct cgpu_info *cgpu)
  156. {
  157. struct bitfury_device * const bitfury = cgpu->device_data;
  158. struct api_data *root = bitfury_api_device_detail(cgpu);
  159. root = api_add_uint(root, "Slot", &(bitfury->slot), false);
  160. return root;
  161. }
  162. struct device_drv bfsb_drv = {
  163. .dname = "bfsb",
  164. .name = "BSB",
  165. .drv_detect = bfsb_detect,
  166. .minerloop = minerloop_async,
  167. .job_prepare = bitfury_job_prepare,
  168. .thread_init = bfsb_init,
  169. .poll = bitfury_do_io,
  170. .job_start = bitfury_noop_job_start,
  171. .job_process_results = bitfury_job_process_results,
  172. .get_api_extra_device_detail = bfsb_api_device_detail,
  173. .get_api_extra_device_status = bitfury_api_device_status,
  174. .set_device = bitfury_set_device,
  175. .thread_disable = bfsb_disable,
  176. .thread_enable = bfsb_enable,
  177. .thread_shutdown = bfsb_shutdown,
  178. #ifdef HAVE_CURSES
  179. .proc_wlogprint_status = bitfury_wlogprint_status,
  180. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  181. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  182. #endif
  183. };