driver-jingtian.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2014 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include "deviceapi.h"
  19. #include "driver-aan.h"
  20. #include "logging.h"
  21. #include "lowl-spi.h"
  22. #include "util.h"
  23. static const int jingtian_cs_gpio[] = {14, 15, 18};
  24. static const int jingtian_max_cs = 1 << (sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio));
  25. static const uint8_t jingtian_pre_header[] = {0xb5, 0xb5};
  26. BFG_REGISTER_DRIVER(jingtian_drv)
  27. static
  28. bool jingtian_spi_txrx(struct spi_port * const port)
  29. {
  30. if (*port->chipselect_current != port->chipselect)
  31. {
  32. unsigned cs_set_low = 0, cs_set_high = 0, cur_cs_bit;
  33. bool bit_desired;
  34. for (int i = 0; i < sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio); ++i)
  35. {
  36. cur_cs_bit = (1 << i);
  37. bit_desired = (port->chipselect & cur_cs_bit);
  38. if (bit_desired == (bool)(*port->chipselect_current & cur_cs_bit))
  39. // No change needed
  40. continue;
  41. if (bit_desired)
  42. cs_set_high |= (1 << jingtian_cs_gpio[i]);
  43. else
  44. cs_set_low |= (1 << jingtian_cs_gpio[i]);
  45. }
  46. if (cs_set_low)
  47. bfg_gpio_set_low(cs_set_low);
  48. if (cs_set_high)
  49. bfg_gpio_set_high(cs_set_high);
  50. if (opt_dev_protocol)
  51. applog(LOG_DEBUG, "%s(%p): CS %d", __func__, port, port->chipselect);
  52. *port->chipselect_current = port->chipselect;
  53. }
  54. if (opt_dev_protocol)
  55. {
  56. char x[(spi_getbufsz(port) * 2) + 1];
  57. bin2hex(x, spi_gettxbuf(port), spi_getbufsz(port));
  58. applog(LOG_DEBUG, "%s(%p): %cX %s", __func__, port, 'T', x);
  59. }
  60. bool rv = linux_spi_txrx(port);
  61. if (opt_dev_protocol)
  62. {
  63. char x[(spi_getbufsz(port) * 2) + 1];
  64. bin2hex(x, spi_getrxbuf(port), spi_getbufsz(port));
  65. applog(LOG_DEBUG, "%s(%p): %cX %s", __func__, port, 'R', x);
  66. }
  67. return rv;
  68. }
  69. static
  70. void jingtian_precmd(struct spi_port * const spi)
  71. {
  72. spi_emit_buf(spi, jingtian_pre_header, sizeof(jingtian_pre_header));
  73. }
  74. static
  75. struct aan_hooks jingtian_hooks = {
  76. .precmd = jingtian_precmd,
  77. .read_reg = aan_read_reg_direct,
  78. };
  79. static
  80. void jingtian_common_init(void)
  81. {
  82. RUNONCE();
  83. spi_init();
  84. for (int i = 0; i < sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio); ++i)
  85. bfg_gpio_setpin_output(jingtian_cs_gpio[i]);
  86. }
  87. static
  88. bool jingtian_detect_one(const char * const devpath)
  89. {
  90. int found = 0, chips;
  91. jingtian_common_init();
  92. const int fd = open(devpath, O_RDWR);
  93. if (fd < 0)
  94. applogr(false, LOG_DEBUG, "%s: Failed to open %s", jingtian_drv.dname, devpath);
  95. struct cgpu_info *cgpu, *prev_cgpu = NULL;
  96. struct spi_port *spi;
  97. int * const chipselect_current = malloc(sizeof(*spi->chipselect_current));
  98. *chipselect_current = -1;
  99. int devpath_len = strlen(devpath);
  100. int chipcount[jingtian_max_cs];
  101. struct spi_port *spi_a[jingtian_max_cs];
  102. for (int i = 0; i < jingtian_max_cs; ++i)
  103. {
  104. spi = spi_a[i] = calloc(sizeof(*spi), 1);
  105. spi->repr = malloc(devpath_len + 0x10);
  106. sprintf((void*)spi->repr, "%s(cs%d)", devpath, i);
  107. spi->txrx = jingtian_spi_txrx;
  108. spi->userp = &jingtian_hooks;
  109. spi->fd = fd;
  110. spi->speed = 4000000;
  111. spi->delay = 0;
  112. spi->mode = 1;
  113. spi->bits = 8;
  114. spi->chipselect = i;
  115. spi->chipselect_current = chipselect_current;
  116. }
  117. aan_detect_spi(chipcount, spi_a, jingtian_max_cs);
  118. for (int i = 0; i < jingtian_max_cs; ++i)
  119. {
  120. chips = chipcount[i];
  121. free((void*)spi_a[i]->repr);
  122. spi_a[i]->repr = NULL;
  123. if (chips <= 0)
  124. {
  125. free(spi_a[i]);
  126. continue;
  127. }
  128. cgpu = malloc(sizeof(*cgpu));
  129. *cgpu = (struct cgpu_info){
  130. .drv = &jingtian_drv,
  131. .procs = chips,
  132. .threads = prev_cgpu ? 0 : 1,
  133. .device_data = spi_a[i],
  134. .device_path = strdup(devpath),
  135. .set_device_funcs = aan_set_device_funcs,
  136. };
  137. add_cgpu_slave(cgpu, prev_cgpu);
  138. prev_cgpu = cgpu;
  139. found += chips;
  140. }
  141. close(fd);
  142. if (!found)
  143. free(chipselect_current);
  144. return found;
  145. }
  146. static
  147. int jingtian_detect_auto(void)
  148. {
  149. return jingtian_detect_one("/dev/spidev0.0") ? 1 : 0;
  150. }
  151. static
  152. void jingtian_detect(void)
  153. {
  154. generic_detect(&jingtian_drv, jingtian_detect_one, jingtian_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  155. }
  156. static
  157. bool jingtian_init(struct thr_info * const master_thr)
  158. {
  159. struct cgpu_info * const master_dev = master_thr->cgpu;
  160. const char * const devpath = master_dev->device_path;
  161. const int fd = open(devpath, O_RDWR);
  162. if (fd < 0)
  163. applogr(false, LOG_ERR, "%s: Failed to open %s", master_dev->dev_repr, devpath);
  164. for_each_managed_proc(proc, master_dev)
  165. {
  166. struct spi_port * const spi = proc->device_data;
  167. spi->fd = fd;
  168. }
  169. return aan_init(master_thr);
  170. }
  171. struct device_drv jingtian_drv = {
  172. .dname = "jingtian",
  173. .name = "JTN",
  174. .drv_detect = jingtian_detect,
  175. .thread_init = jingtian_init,
  176. .minerloop = minerloop_queue,
  177. .queue_append = aan_queue_append,
  178. .queue_flush = aan_queue_flush,
  179. .poll = aan_poll,
  180. };