driver-aan.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 void aan_spi_parse_rx(struct spi_port *);
  30. static
  31. void aan_spi_cmd_queue(struct spi_port * const spi, const uint8_t cmd, const uint8_t chip, const void * const data, const size_t datalen)
  32. {
  33. const struct aan_hooks * const hooks = spi->userp;
  34. const uint8_t cmdbuf[2] = {cmd, chip};
  35. hooks->precmd(spi);
  36. spi_emit_buf(spi, cmdbuf, sizeof(cmdbuf));
  37. if (datalen)
  38. spi_emit_buf(spi, data, datalen);
  39. }
  40. static
  41. bool aan_spi_txrx(struct spi_port * const spi)
  42. {
  43. if (unlikely(!spi_txrx(spi)))
  44. return false;
  45. aan_spi_parse_rx(spi);
  46. spi_clear_buf(spi);
  47. return true;
  48. }
  49. static
  50. bool aan_spi_cmd_send(struct spi_port * const spi, const uint8_t cmd, const uint8_t chip, const void * const data, const size_t datalen)
  51. {
  52. aan_spi_cmd_queue(spi, cmd, chip, data, datalen);
  53. return aan_spi_txrx(spi);
  54. }
  55. static
  56. bool aan_spi_cmd_resp(struct spi_port * const spi, const uint8_t cmd, const uint8_t chip, const struct timeval * const tvp_timeout)
  57. {
  58. const uint8_t cmdbuf[2] = {cmd, chip};
  59. spi_emit_nop(spi, 2);
  60. uint8_t * const rx = spi_getrxbuf(spi);
  61. while (true)
  62. {
  63. if (unlikely(!spi_txrx(spi)))
  64. return false;
  65. if (!memcmp(rx, cmdbuf, 2))
  66. break;
  67. aan_spi_parse_rx(spi);
  68. if (unlikely(tvp_timeout && timer_passed(tvp_timeout, NULL)))
  69. return false;
  70. }
  71. spi_clear_buf(spi);
  72. return true;
  73. }
  74. static
  75. bool aan_spi_cmd(struct spi_port * const spi, const uint8_t cmd, const uint8_t chip, const void * const data, const size_t datalen, const struct timeval * const tvp_timeout)
  76. {
  77. if (!aan_spi_cmd_send(spi, cmd, chip, data, datalen))
  78. return false;
  79. if (!aan_spi_cmd_resp(spi, cmd, chip, tvp_timeout))
  80. return false;
  81. return true;
  82. }
  83. static
  84. int aan_spi_autoaddress(struct spi_port * const spi, const struct timeval * const tvp_timeout)
  85. {
  86. if (!aan_spi_cmd(spi, AAN_BIST_START, AAN_ALL_CHIPS, NULL, 0, tvp_timeout))
  87. applogr(-1, LOG_DEBUG, "%s: %s failed", __func__, "AAN_BIST_START");
  88. spi_emit_nop(spi, 2);
  89. if (!spi_txrx(spi))
  90. applogr(-1, LOG_DEBUG, "%s: %s failed", __func__, "spi_txrx");
  91. uint8_t * const rx = spi_getrxbuf(spi);
  92. return rx[1];
  93. }
  94. int aan_detect_spi(struct spi_port * const spi)
  95. {
  96. struct timeval tv_timeout;
  97. timer_set_delay_from_now(&tv_timeout, AAN_PROBE_TIMEOUT_US);
  98. if (!aan_spi_cmd(spi, AAN_RESET, AAN_ALL_CHIPS, NULL, 0, &tv_timeout))
  99. return -1;
  100. return aan_spi_autoaddress(spi, &tv_timeout);
  101. }
  102. static
  103. void aan_spi_parse_rx(struct spi_port * const spi)
  104. {
  105. // TODO
  106. }