driver-jingtian.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_spi_enable_gpio = 25;
  25. static const int jingtian_max_cs = 1 << (sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio));
  26. static const uint8_t jingtian_pre_header[] = {0xb5, 0xb5};
  27. #define JINGTIAN_REGISTER_EXTRA_SIZE 2
  28. BFG_REGISTER_DRIVER(jingtian_drv)
  29. static
  30. bool jingtian_spi_txrx(struct spi_port * const port)
  31. {
  32. if (*port->chipselect_current != port->chipselect)
  33. {
  34. unsigned cs_set_low = 0, cs_set_high = 0, cur_cs_bit;
  35. bool bit_desired;
  36. for (int i = 0; i < sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio); ++i)
  37. {
  38. cur_cs_bit = (1 << i);
  39. bit_desired = (port->chipselect & cur_cs_bit);
  40. if (bit_desired == (bool)(*port->chipselect_current & cur_cs_bit))
  41. // No change needed
  42. continue;
  43. if (bit_desired)
  44. cs_set_high |= (1 << jingtian_cs_gpio[i]);
  45. else
  46. cs_set_low |= (1 << jingtian_cs_gpio[i]);
  47. }
  48. bfg_gpio_set_high(1 << jingtian_spi_enable_gpio);
  49. if (cs_set_low)
  50. bfg_gpio_set_low(cs_set_low);
  51. if (cs_set_high)
  52. bfg_gpio_set_high(cs_set_high);
  53. bfg_gpio_set_low(1 << jingtian_spi_enable_gpio);
  54. if (opt_dev_protocol)
  55. applog(LOG_DEBUG, "%s(%p): CS %d", __func__, port, port->chipselect);
  56. *port->chipselect_current = port->chipselect;
  57. }
  58. if (opt_dev_protocol)
  59. {
  60. char x[(spi_getbufsz(port) * 2) + 1];
  61. bin2hex(x, spi_gettxbuf(port), spi_getbufsz(port));
  62. applog(LOG_DEBUG, "%s(%p): %cX %s", __func__, port, 'T', x);
  63. }
  64. bool rv = linux_spi_txrx(port);
  65. if (opt_dev_protocol)
  66. {
  67. char x[(spi_getbufsz(port) * 2) + 1];
  68. bin2hex(x, spi_getrxbuf(port), spi_getbufsz(port));
  69. applog(LOG_DEBUG, "%s(%p): %cX %s", __func__, port, 'R', x);
  70. }
  71. return rv;
  72. }
  73. static
  74. void jingtian_precmd(struct spi_port * const spi)
  75. {
  76. spi_emit_buf(spi, jingtian_pre_header, sizeof(jingtian_pre_header));
  77. }
  78. static
  79. bool jingtian_read_reg(struct spi_port * const spi, const uint8_t chip, void * const out_buf, const struct timeval * const tvp_timeout)
  80. {
  81. if (!aan_read_reg_direct(spi, chip, out_buf, tvp_timeout))
  82. return false;
  83. spi_emit_nop(spi, JINGTIAN_REGISTER_EXTRA_SIZE);
  84. if (!spi_txrx(spi))
  85. applogr(false, LOG_DEBUG, "%s: %s failed", __func__, "spi_txrx");
  86. struct cgpu_info * const dev = spi->cgpu;
  87. if (unlikely(!dev))
  88. return true;
  89. struct cgpu_info * const proc = aan_proc_for_chipid(dev, chip);
  90. uint8_t * const rx = spi_getrxbuf(spi);
  91. proc->temp = upk_u16be(rx, 0);
  92. return true;
  93. }
  94. static
  95. struct aan_hooks jingtian_hooks = {
  96. .precmd = jingtian_precmd,
  97. .read_reg = jingtian_read_reg,
  98. };
  99. static
  100. void jingtian_common_init(void)
  101. {
  102. RUNONCE();
  103. spi_init();
  104. for (int i = 0; i < sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio); ++i)
  105. bfg_gpio_setpin_output(jingtian_cs_gpio[i]);
  106. bfg_gpio_setpin_output(jingtian_spi_enable_gpio);
  107. }
  108. static
  109. bool jingtian_detect_one(const char * const devpath)
  110. {
  111. int found = 0, chips;
  112. jingtian_common_init();
  113. const int fd = open(devpath, O_RDWR);
  114. if (fd < 0)
  115. applogr(false, LOG_DEBUG, "%s: Failed to open %s", jingtian_drv.dname, devpath);
  116. struct cgpu_info *cgpu, *prev_cgpu = NULL;
  117. struct spi_port *spi;
  118. int * const chipselect_current = malloc(sizeof(*spi->chipselect_current));
  119. *chipselect_current = -1;
  120. int devpath_len = strlen(devpath);
  121. int chipcount[jingtian_max_cs];
  122. struct spi_port *spi_a[jingtian_max_cs];
  123. for (int i = 0; i < jingtian_max_cs; ++i)
  124. {
  125. spi = spi_a[i] = calloc(sizeof(*spi), 1);
  126. spi->repr = malloc(devpath_len + 0x10);
  127. sprintf((void*)spi->repr, "%s(cs%d)", devpath, i);
  128. spi->txrx = jingtian_spi_txrx;
  129. spi->userp = &jingtian_hooks;
  130. spi->fd = fd;
  131. spi->speed = 4000000;
  132. spi->delay = 0;
  133. spi->mode = 1;
  134. spi->bits = 8;
  135. spi->chipselect = i;
  136. spi->chipselect_current = chipselect_current;
  137. }
  138. aan_detect_spi(chipcount, spi_a, jingtian_max_cs);
  139. for (int i = 0; i < jingtian_max_cs; ++i)
  140. {
  141. chips = chipcount[i];
  142. free((void*)spi_a[i]->repr);
  143. spi_a[i]->repr = NULL;
  144. if (chips <= 0)
  145. {
  146. free(spi_a[i]);
  147. continue;
  148. }
  149. cgpu = malloc(sizeof(*cgpu));
  150. *cgpu = (struct cgpu_info){
  151. .drv = &jingtian_drv,
  152. .procs = chips,
  153. .threads = prev_cgpu ? 0 : 1,
  154. .device_data = spi_a[i],
  155. .device_path = strdup(devpath),
  156. .set_device_funcs = aan_set_device_funcs,
  157. };
  158. spi_a[i]->cgpu = cgpu;
  159. add_cgpu_slave(cgpu, prev_cgpu);
  160. prev_cgpu = cgpu;
  161. found += chips;
  162. }
  163. close(fd);
  164. if (!found)
  165. free(chipselect_current);
  166. return found;
  167. }
  168. static
  169. int jingtian_detect_auto(void)
  170. {
  171. return jingtian_detect_one("/dev/spidev0.0") ? 1 : 0;
  172. }
  173. static
  174. void jingtian_detect(void)
  175. {
  176. generic_detect(&jingtian_drv, jingtian_detect_one, jingtian_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  177. }
  178. static
  179. bool jingtian_init(struct thr_info * const master_thr)
  180. {
  181. struct cgpu_info * const master_dev = master_thr->cgpu;
  182. const char * const devpath = master_dev->device_path;
  183. const int fd = open(devpath, O_RDWR);
  184. if (fd < 0)
  185. applogr(false, LOG_ERR, "%s: Failed to open %s", master_dev->dev_repr, devpath);
  186. for_each_managed_proc(proc, master_dev)
  187. {
  188. struct spi_port * const spi = proc->device_data;
  189. spi->fd = fd;
  190. }
  191. return aan_init(master_thr);
  192. }
  193. struct device_drv jingtian_drv = {
  194. .dname = "jingtian",
  195. .name = "JTN",
  196. .drv_detect = jingtian_detect,
  197. .thread_init = jingtian_init,
  198. .minerloop = minerloop_queue,
  199. .queue_append = aan_queue_append,
  200. .queue_flush = aan_queue_flush,
  201. .poll = aan_poll,
  202. };