driver-aan.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <string.h>
  13. #include "driver-aan.h"
  14. #include "logging.h"
  15. #include "lowl-spi.h"
  16. #include "miner.h"
  17. #include "util.h"
  18. #define AAN_PROBE_TIMEOUT_US 3750000
  19. enum aan_cmd {
  20. AAN_BIST_START = 0x01,
  21. AAN_BIST_FIX = 0x03,
  22. AAN_RESET = 0x04,
  23. AAN_WRITE_JOB = 0x07,
  24. AAN_READ_RESULT = 0x08,
  25. AAN_WRITE_REG = 0x09,
  26. AAN_READ_REG = 0x0a,
  27. AAN_READ_REG_RESP = 0x1a,
  28. };
  29. static
  30. bool aan_spi_cmd(struct spi_port * const spi, const uint8_t cmd, const uint8_t chip, const struct timeval * const tvp_timeout)
  31. {
  32. const struct aan_hooks * const hooks = spi->userp;
  33. const uint8_t cmdbuf[2] = {cmd, chip};
  34. hooks->precmd(spi);
  35. spi_emit_buf(spi, cmdbuf, sizeof(cmdbuf));
  36. if (unlikely(!spi_txrx(spi)))
  37. return false;
  38. spi_clear_buf(spi);
  39. spi_emit_nop(spi, 2);
  40. uint8_t * const rx = spi_getrxbuf(spi);
  41. while (true)
  42. {
  43. if (unlikely(!spi_txrx(spi)))
  44. return false;
  45. if (!memcmp(rx, cmdbuf, 2))
  46. break;
  47. if (unlikely(tvp_timeout && timer_passed(tvp_timeout, NULL)))
  48. return false;
  49. }
  50. spi_clear_buf(spi);
  51. return true;
  52. }
  53. static
  54. int aan_spi_autoaddress(struct spi_port * const spi, const struct timeval * const tvp_timeout)
  55. {
  56. if (!aan_spi_cmd(spi, AAN_BIST_START, AAN_ALL_CHIPS, tvp_timeout))
  57. applogr(-1, LOG_DEBUG, "%s: %s failed", __func__, "AAN_BIST_START");
  58. spi_emit_nop(spi, 2);
  59. if (!spi_txrx(spi))
  60. applogr(-1, LOG_DEBUG, "%s: %s failed", __func__, "spi_txrx");
  61. uint8_t * const rx = spi_getrxbuf(spi);
  62. return rx[1];
  63. }
  64. int aan_detect_spi(struct spi_port * const spi)
  65. {
  66. struct timeval tv_timeout;
  67. timer_set_delay_from_now(&tv_timeout, AAN_PROBE_TIMEOUT_US);
  68. if (!aan_spi_cmd(spi, AAN_RESET, AAN_ALL_CHIPS, &tv_timeout))
  69. return -1;
  70. return aan_spi_autoaddress(spi, &tv_timeout);
  71. }