driver-avalon4.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright 2013-2015 Con Kolivas <kernel@kolivas.org>
  3. * Copyright 2012-2014 Xiangfu <xiangfu@openmobilefree.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #ifndef _AVALON4_H_
  11. #define _AVALON4_H_
  12. #include "util.h"
  13. #include "i2c-context.h"
  14. #ifdef USE_AVALON4
  15. #define AVA4_DEFAULT_FAN_MIN 10 /* % */
  16. #define AVA4_DEFAULT_FAN_MAX 100
  17. /* Percentage required to make sure fan starts spinning, then we can go down */
  18. #define AVA4_DEFAULT_FAN_START 15
  19. #define AVA4_FREEZESAFE_FAN 10
  20. #define AVA4_DEFAULT_TEMP_TARGET 65
  21. #define AVA4_DEFAULT_TEMP_OVERHEAT 85
  22. #define AVA4_MM40_TEMP_TARGET 42
  23. #define AVA4_MM40_TEMP_OVERHEAT 65
  24. #define AVA4_MM60_TEMP_FREQADJ 70
  25. #define AVA4_DEFAULT_VOLTAGE_MIN 4000
  26. #define AVA4_DEFAULT_VOLTAGE_MAX 9000
  27. #define AVA4_FREEZESAFE_VOLTAGE 4000
  28. #define AVA4_DEFAULT_FREQUENCY_MIN 100
  29. #define AVA4_DEFAULT_FREQUENCY_MAX 1000
  30. #define AVA4_FREEZESAFE_FREQUENCY 100
  31. #define AVA4_MM60_FREQUENCY_MAX 500
  32. #define AVA4_DEFAULT_MODULARS 7 /* Only support 6 modules maximum with one AUC */
  33. #define AVA4_DEFAULT_MINER_MAX 10
  34. #define AVA4_DEFAULT_ASIC_MAX 40
  35. #define AVA4_DEFAULT_ADC_MAX 6 /* RNTC1-4, VCC12, VCC3VC */
  36. #define AVA4_DEFAULT_PLL_MAX 7
  37. #define AVA4_DEFAULT_MINER_CNT 10
  38. #define AVA4_DEFAULT_ASIC_CNT 4
  39. #define AVA4_MM50_MINER_CNT 2
  40. #define AVA4_MM50_ASIC_CNT 16
  41. #define AVA4_MM60_MINER_CNT 2
  42. #define AVA4_MM60_ASIC_CNT 40
  43. #define AVA4_DEFAULT_VOLTAGE 6875
  44. #define AVA4_DEFAULT_FREQUENCY 200
  45. #define AVA4_DEFAULT_POLLING_DELAY 20 /* ms */
  46. #define AVA4_DEFAULT_ADJ_TIMES 6
  47. #define AVA4_DEFAULT_NTCB 3450
  48. #define AVA4_DEFAULT_NCHECK true
  49. #define AVA4_DEFAULT_SPEED_BINGO 255
  50. #define AVA4_DEFAULT_SPEED_ERROR 3
  51. #define AVA4_DEFAULT_SMARTSPEED_OFF 0
  52. #define AVA4_DEFAULT_SMARTSPEED_MODE1 1
  53. #define AVA4_DEFAULT_SMARTSPEED_MODE2 2
  54. #define AVA4_DEFAULT_SMARTSPEED_MODE3 3
  55. #define AVA4_DEFAULT_SMART_SPEED (AVA4_DEFAULT_SMARTSPEED_MODE3)
  56. #define AVA4_DEFAULT_IIC_DETECT false
  57. #define AVA4_DH_INC 0.03
  58. #define AVA4_DH_DEC 0.002
  59. #define AVA4_PWM_MAX 0x3FF
  60. #define AVA4_ADC_MAX 0x3FF
  61. #define AVA4_DRV_DIFFMAX 1024
  62. #define AVA4_AUC_VER_LEN 12 /* Version length: 12 (AUC-YYYYMMDD) */
  63. #define AVA4_AUC_SPEED 400000
  64. #define AVA4_AUC_XDELAY 19200 /* 4800 = 1ms in AUC (11U14) */
  65. #define AVA4_AUC_P_SIZE 64
  66. #define AVA4_MOD_CUSTOM 0x0
  67. #define AVA4_MOD_ECO 0x1
  68. #define AVA4_MOD_NORMAL 0x2
  69. #define AVA4_MOD_TURBO 0x3
  70. #define AVA4_CONNECTER_AUC 1
  71. #define AVA4_CONNECTER_IIC 2
  72. /* Avalon4 protocol package type from MM protocol.h
  73. * https://github.com/Canaan-Creative/MM/blob/avalon4/firmware/protocol.h */
  74. #define AVA4_MM_VER_LEN 15
  75. #define AVA4_MM_DNA_LEN 8
  76. #define AVA4_H1 'C'
  77. #define AVA4_H2 'N'
  78. #define AVA4_P_COINBASE_SIZE (6 * 1024 + 64)
  79. #define AVA4_P_MERKLES_COUNT 30
  80. #define AVA4_P_COUNT 40
  81. #define AVA4_P_DATA_LEN 32
  82. /* Broadcase with block iic_write*/
  83. #define AVA4_P_DETECT 0x10
  84. /* Broadcase With non-block iic_write*/
  85. #define AVA4_P_STATIC 0x11
  86. #define AVA4_P_JOB_ID 0x12
  87. #define AVA4_P_COINBASE 0x13
  88. #define AVA4_P_MERKLES 0x14
  89. #define AVA4_P_HEADER 0x15
  90. #define AVA4_P_TARGET 0x16
  91. /* Broadcase or Address */
  92. #define AVA4_P_SET 0x20
  93. #define AVA4_P_FINISH 0x21
  94. #define AVA4_P_SET_VOLT 0x22
  95. #define AVA4_P_SET_FREQ 0x23
  96. /* Have to with I2C address */
  97. #define AVA4_P_POLLING 0x30
  98. #define AVA4_P_REQUIRE 0x31
  99. #define AVA4_P_TEST 0x32
  100. #define AVA4_P_RSTMMTX 0x33
  101. #define AVA4_P_GET_VOLT 0x34
  102. /* Back to host */
  103. #define AVA4_P_ACKDETECT 0x40
  104. #define AVA4_P_STATUS 0x41
  105. #define AVA4_P_NONCE 0x42
  106. #define AVA4_P_TEST_RET 0x43
  107. #define AVA4_P_STATUS_LW 0x44
  108. #define AVA4_P_STATUS_HW 0x45
  109. #define AVA4_P_STATUS_VOLT 0x46
  110. #define AVA4_P_STATUS_MA 0x47
  111. #define AVA4_P_STATUS_M 0x48
  112. #define AVA4_MODULE_BROADCAST 0
  113. /* Endof Avalon4 protocol package type */
  114. #define AVA4_MM40_PREFIXSTR "40"
  115. #define AVA4_MM41_PREFIXSTR "41"
  116. #define AVA4_MM50_PREFIXSTR "50"
  117. #define AVA4_MM60_PREFIXSTR "60"
  118. #define AVA4_MM_VERNULL "NONE"
  119. #define AVA4_TYPE_MM40 40
  120. #define AVA4_TYPE_MM41 41
  121. #define AVA4_TYPE_MM50 50
  122. #define AVA4_TYPE_MM60 60
  123. #define AVA4_TYPE_NULL 00
  124. #define AVA4_IIC_RESET 0xa0
  125. #define AVA4_IIC_INIT 0xa1
  126. #define AVA4_IIC_DEINIT 0xa2
  127. #define AVA4_IIC_XFER 0xa5
  128. #define AVA4_IIC_INFO 0xa6
  129. #define AVA4_FREQ_INIT_MODE 0x0
  130. #define AVA4_FREQ_CUTOFF_MODE 0x1
  131. #define AVA4_FREQ_TEMPADJ_MODE 0x2
  132. #define AVA4_FREQ_PLLADJ_MODE 0x3
  133. /* pll check range [0, 7680], 0 means turn off check */
  134. #define AVA4_DEFAULT_LEAST_PLL 768
  135. #define AVA4_DEFAULT_MOST_PLL 256
  136. /* seconds */
  137. #define AVA4_DEFAULT_FDEC_TIME 60.0
  138. #define AVA4_DEFAULT_FINC_TIME 1200.0
  139. #define AVA4_DEFAULT_FAVG_TIME (15 * 60.0)
  140. #define AVA4_DEFAULT_FREQADJ_TIME 60
  141. #define AVA4_DEFAULT_DELTA_T 0
  142. #define AVA4_DEFAULT_DELTA_FREQ 100
  143. struct avalon4_pkg {
  144. uint8_t head[2];
  145. uint8_t type;
  146. uint8_t opt;
  147. uint8_t idx;
  148. uint8_t cnt;
  149. uint8_t data[32];
  150. uint8_t crc[2];
  151. };
  152. #define avalon4_ret avalon4_pkg
  153. struct avalon4_info {
  154. cglock_t update_lock;
  155. int polling_first;
  156. int polling_err_cnt[AVA4_DEFAULT_MODULARS];
  157. int xfer_err_cnt;
  158. int pool_no;
  159. struct pool pool0;
  160. struct pool pool1;
  161. struct pool pool2;
  162. struct timeval last_fan;
  163. struct timeval last_stratum;
  164. char auc_version[AVA4_AUC_VER_LEN + 1];
  165. int auc_speed;
  166. int auc_xdelay;
  167. int auc_temp;
  168. int mm_count;
  169. uint8_t connecter;
  170. unsigned int set_frequency[AVA4_DEFAULT_MODULARS][3];
  171. unsigned int set_smart_frequency[AVA4_DEFAULT_MODULARS][3];
  172. int set_frequency_i[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX][AVA4_DEFAULT_ASIC_MAX][3];
  173. int set_voltage[AVA4_DEFAULT_MODULARS];
  174. uint16_t set_voltage_i[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX];
  175. int8_t set_voltage_offset[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX];
  176. int mod_type[AVA4_DEFAULT_MODULARS];
  177. bool enable[AVA4_DEFAULT_MODULARS];
  178. struct timeval elapsed[AVA4_DEFAULT_MODULARS];
  179. struct timeval firsthash;
  180. char mm_version[AVA4_DEFAULT_MODULARS][AVA4_MM_VER_LEN + 1];
  181. uint8_t mm_dna[AVA4_DEFAULT_MODULARS][AVA4_MM_DNA_LEN + 1];
  182. int get_voltage[AVA4_DEFAULT_MODULARS];
  183. int get_voltage_i[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX];
  184. int get_frequency[AVA4_DEFAULT_MODULARS];
  185. int power_good[AVA4_DEFAULT_MODULARS];
  186. int error_code[AVA4_DEFAULT_MODULARS];
  187. int fan_pct[AVA4_DEFAULT_MODULARS];
  188. int fan[AVA4_DEFAULT_MODULARS];
  189. int temp[AVA4_DEFAULT_MODULARS];
  190. int led_red[AVA4_DEFAULT_MODULARS];
  191. uint16_t adc[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_ADC_MAX];
  192. uint16_t pll_sel[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_PLL_MAX];
  193. uint64_t local_works[AVA4_DEFAULT_MODULARS];
  194. uint64_t local_works_i[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX];
  195. uint64_t hw_works[AVA4_DEFAULT_MODULARS];
  196. uint64_t hw_works_i[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX];
  197. uint32_t local_work[AVA4_DEFAULT_MODULARS];
  198. uint32_t hw_work[AVA4_DEFAULT_MODULARS];
  199. uint32_t lw5[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_ADJ_TIMES];
  200. uint32_t lw5_i[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX][AVA4_DEFAULT_ADJ_TIMES];
  201. uint32_t hw5[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_ADJ_TIMES];
  202. uint32_t hw5_i[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX][AVA4_DEFAULT_ADJ_TIMES];
  203. int i_5s;
  204. struct timeval last_30s;
  205. struct timeval last_5s;
  206. struct timeval last_finc[AVA4_DEFAULT_MODULARS];
  207. struct timeval last_fdec[AVA4_DEFAULT_MODULARS];
  208. struct timeval last_favg[AVA4_DEFAULT_MODULARS];
  209. struct timeval last_fadj;
  210. struct timeval last_tcheck;
  211. int matching_work[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX];
  212. int chipmatching_work[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX][AVA4_DEFAULT_ASIC_MAX];
  213. uint8_t saved[AVA4_DEFAULT_MODULARS];
  214. uint8_t adjflag[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX];
  215. uint8_t cutoff[AVA4_DEFAULT_MODULARS];
  216. uint8_t miner_count[AVA4_DEFAULT_MODULARS];
  217. uint8_t asic_count[AVA4_DEFAULT_MODULARS];
  218. int ntime_offset[AVA4_DEFAULT_MODULARS];
  219. bool autov[AVA4_DEFAULT_MODULARS];
  220. uint8_t ma_sum[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX][AVA4_DEFAULT_ASIC_MAX];
  221. uint32_t newnonce;
  222. uint32_t total_asics[AVA4_DEFAULT_MODULARS];
  223. int toverheat[AVA4_DEFAULT_MODULARS];
  224. int temp_target[AVA4_DEFAULT_MODULARS];
  225. uint8_t speed_bingo[AVA4_DEFAULT_MODULARS];
  226. uint8_t speed_error[AVA4_DEFAULT_MODULARS];
  227. uint32_t freq_mode[AVA4_DEFAULT_MODULARS];
  228. struct i2c_ctx *i2c_slaves[AVA4_DEFAULT_MODULARS];
  229. int last_maxtemp[AVA4_DEFAULT_MODULARS];
  230. };
  231. struct avalon4_iic_info {
  232. uint8_t iic_op;
  233. union {
  234. uint32_t aucParam[2];
  235. uint8_t slave_addr;
  236. } iic_param;
  237. };
  238. #define AVA4_WRITE_SIZE (sizeof(struct avalon4_pkg))
  239. #define AVA4_READ_SIZE AVA4_WRITE_SIZE
  240. #define AVA4_SEND_OK 0
  241. #define AVA4_SEND_ERROR -1
  242. extern char *set_avalon4_fan(char *arg);
  243. extern char *set_avalon4_temp(char *arg);
  244. extern char *set_avalon4_freq(char *arg);
  245. extern char *set_avalon4_voltage(char *arg);
  246. extern bool opt_avalon4_autov;
  247. extern bool opt_avalon4_freezesafe;
  248. extern int opt_avalon4_temp_target;
  249. extern int opt_avalon4_overheat;
  250. extern int opt_avalon4_polling_delay;
  251. extern int opt_avalon4_aucspeed;
  252. extern int opt_avalon4_aucxdelay;
  253. extern int opt_avalon4_ntime_offset;
  254. extern int opt_avalon4_miningmode;
  255. extern int opt_avalon4_ntcb;
  256. extern int opt_avalon4_freq_min;
  257. extern int opt_avalon4_freq_max;
  258. extern bool opt_avalon4_noncecheck;
  259. extern int opt_avalon4_smart_speed;
  260. extern int opt_avalon4_speed_bingo;
  261. extern int opt_avalon4_speed_error;
  262. extern int opt_avalon4_least_pll_check;
  263. extern int opt_avalon4_most_pll_check;
  264. extern bool opt_avalon4_iic_detect;
  265. extern int opt_avalon4_freqadj_time;
  266. extern int opt_avalon4_delta_temp;
  267. extern int opt_avalon4_delta_freq;
  268. extern int opt_avalon4_freqadj_temp;
  269. #endif /* USE_AVALON4 */
  270. #endif /* _AVALON4_H_ */