driver-avalon-miner.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2014-2015 Mikeqin <Fengling.Qin@gmail.com>
  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. #ifndef _AVALON_MINER_H_
  10. #define _AVALON_MINER_H_
  11. #include "util.h"
  12. #ifdef USE_AVALON_MINER
  13. #define AVAM_DEFAULT_ASIC_COUNT 5
  14. #define AVAM_DEFAULT_ARRAY_SIZE (3 + 2) /* This is from the A3222 datasheet. 3 quequed work + 1 new work + 1 auxiliary, because the device may buffer more work */
  15. #define AVAM_DEFAULT_FREQUENCY_MIN 100
  16. #define AVAM_DEFAULT_FREQUENCY_MAX 400
  17. #define AVAM_DEFAULT_FREQUENCY 200
  18. #define AVAM_DEFAULT_VOLTAGE_MIN 5000
  19. #define AVAM_DEFAULT_VOLTAGE_MAX 9000
  20. #define AVAM_DEFAULT_VOLTAGE 6500
  21. #define AVAM_DEFAULT_SPISPEED 1000000
  22. #define AVAM_ASIC_ALL 0
  23. #define CAL_DELAY(freq) (100 * AVAM_ASIC_TIMEOUT_100M / (freq) / 4)
  24. /* 2 ^ 32 * 1000 / (10 ^ 8 * 3968 / 65.0) ~= 703 ms */
  25. #define AVAM_ASIC_TIMEOUT_100M 703
  26. #define AVAM_DEFAULT_MOV_TIMES 6
  27. #define AVAM_DEFAULT_ADJ_INTERVAL 40
  28. #define AVAM_HW_HIGH 20
  29. #define AVAM_HW_LOW 6
  30. /* Avalon4 protocol package type from MM protocol.h
  31. * https://github.com/Canaan-Creative/MM/blob/avalon4/firmware/protocol.h */
  32. #define AVAM_MM_VER_LEN 15
  33. #define AVAM_MM_DNA_LEN 8
  34. #define AVAM_H1 'C'
  35. #define AVAM_H2 'N'
  36. #define AVAM_P_COUNT 40
  37. #define AVAM_P_DATA_LEN 32
  38. #define AVAM_P_DETECT 0x10
  39. #define AVAM_P_SET_VOLT 0x22
  40. #define AVAM_P_SET_FREQ 0x23
  41. #define AVAM_P_WORK 0x24
  42. #define AVAM_P_SETM 0x25
  43. #define AVAM_P_POLLING 0x30
  44. #define AVAM_P_REQUIRE 0x31
  45. #define AVAM_P_TEST 0x32
  46. #define AVAM_P_GET_FREQ 0x33
  47. #define AVAM_P_ACKDETECT 0x40
  48. #define AVAM_P_STATUS_M 0x41
  49. #define AVAM_P_NONCE_M 0x42
  50. #define AVAM_P_TEST_RET 0x43
  51. #define AVAM_P_STATUS_FREQ 0x44
  52. struct avalonm_pkg {
  53. uint8_t head[2];
  54. uint8_t type;
  55. uint8_t opt;
  56. uint8_t idx;
  57. uint8_t cnt;
  58. uint8_t data[32];
  59. uint8_t crc[2];
  60. };
  61. #define avalonm_ret avalonm_pkg
  62. struct avalonm_info {
  63. struct thr_info *thr;
  64. pthread_t process_thr;
  65. pthread_mutex_t lock;
  66. pthread_mutex_t qlock;
  67. cgsem_t qsem;
  68. uint32_t delay_ms;
  69. int power_on;
  70. unsigned char dna[AVAM_MM_DNA_LEN];
  71. unsigned char ver[AVAM_MM_VER_LEN + 1];
  72. uint32_t asic_cnts;
  73. uint32_t set_frequency[AVAM_DEFAULT_ASIC_COUNT][3];
  74. uint32_t opt_freq[AVAM_DEFAULT_ASIC_COUNT][3];
  75. uint32_t get_frequency[AVAM_DEFAULT_ASIC_COUNT][3];
  76. int set_voltage;
  77. int opt_voltage;
  78. uint32_t nonce_cnts;
  79. uint8_t usbfifo_cnt;
  80. uint8_t workfifo_cnt;
  81. uint8_t noncefifo_cnt;
  82. uint32_t crcerr_cnt;
  83. uint32_t power_good;
  84. uint32_t spi_speed;
  85. uint32_t led_status;
  86. uint32_t fan_pwm;
  87. uint32_t get_voltage;
  88. uint8_t freq_update;
  89. uint8_t freq_set;
  90. int hw_work[AVAM_DEFAULT_ASIC_COUNT];
  91. uint64_t matching_work[AVAM_DEFAULT_ASIC_COUNT];
  92. uint32_t adc[3];
  93. struct timeval elapsed;
  94. struct timeval lasttime;
  95. struct timeval lastadj;
  96. uint8_t time_i;
  97. int hw_work_i[AVAM_DEFAULT_ASIC_COUNT][AVAM_DEFAULT_MOV_TIMES];
  98. };
  99. #define AVAM_WRITE_SIZE (sizeof(struct avalonm_pkg))
  100. #define AVAM_READ_SIZE AVAM_WRITE_SIZE
  101. #define AVAM_SEND_OK 0
  102. #define AVAM_SEND_ERROR -1
  103. #define FLAG_SET(val, bit) ((val) |= (1 << (bit)))
  104. #define FLAG_CLEAR(val, bit) ((val) &= ~(1 << (bit)))
  105. #define FLAG_GET(val, bit) (((val) >> (bit)) & 1)
  106. extern char *set_avalonm_freq(char *arg);
  107. extern uint8_t opt_avalonm_ntime_offset;
  108. extern char *set_avalonm_voltage(char *arg);
  109. extern uint32_t opt_avalonm_spispeed;
  110. extern bool opt_avalonm_autof;
  111. #endif /* USE_AVALON_MINER */
  112. #endif /* _AVALON_MINER_H_ */