driver-bfsb.c 5.6 KB

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