driver-avalon8.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright 2017 xuzhenxing <xuzhenxing@canaan-creative.com>
  3. * Copyright 2016-2017 Mikeqin <Fengling.Qin@gmail.com>
  4. * Copyright 2016 Con Kolivas <kernel@kolivas.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #ifndef _AVALON8_H_
  12. #define _AVALON8_H_
  13. #include "util.h"
  14. #include "i2c-context.h"
  15. #ifdef USE_AVALON8
  16. #define AVA8_FREQUENCY_MAX 1404
  17. #define AVA8_DEFAULT_FAN_MIN 5 /* % */
  18. #define AVA8_DEFAULT_FAN_MAX 100
  19. #define AVA8_DEFAULT_TEMP_TARGET 90
  20. #define AVA8_DEFAULT_TEMP_OVERHEAT 105
  21. #define AVA8_DEFAULT_VOLTAGE_LEVEL_MIN -15
  22. #define AVA8_DEFAULT_VOLTAGE_LEVEL_MAX 15
  23. #define AVA8_INVALID_VOLTAGE_LEVEL -16
  24. #define AVA8_DEFAULT_VOLTAGE_LEVEL_OFFSET_MIN -2
  25. #define AVA8_DEFAULT_VOLTAGE_LEVEL_OFFSET 0
  26. #define AVA8_DEFAULT_VOLTAGE_LEVEL_OFFSET_MAX 1
  27. #define AVA8_INVALID_ASIC_OTP -1
  28. #define AVA8_DEFAULT_FACTORY_INFO_0_MIN -15
  29. #define AVA8_DEFAULT_FACTORY_INFO_0 0
  30. #define AVA8_DEFAULT_FACTORY_INFO_0_MAX 15
  31. #define AVA8_DEFAULT_FACTORY_INFO_0_CNT 1
  32. #define AVA8_DEFAULT_FACTORY_INFO_0_IGNORE 16
  33. #define AVA8_DEFAULT_FACTORY_INFO_1_CNT 3
  34. #define AVA8_DEFAULT_OVERCLOCKING_OFF 0
  35. #define AVA8_DEFAULT_OVERCLOCKING_ON 1
  36. #define AVA8_DEFAULT_FREQUENCY_0M 0
  37. #define AVA8_DEFAULT_FREQUENCY_650M 650
  38. #define AVA8_DEFAULT_FREQUENCY_725M 725
  39. #define AVA8_DEFAULT_FREQUENCY_775M 775
  40. #define AVA8_DEFAULT_FREQUENCY_850M 850
  41. #define AVA8_DEFAULT_FREQUENCY_MAX 1200
  42. #define AVA8_DEFAULT_FREQUENCY (AVA8_DEFAULT_FREQUENCY_MAX)
  43. #define AVA8_DEFAULT_FREQUENCY_SEL 3
  44. #define AVA8_DEFAULT_MODULARS 7 /* Only support 6 modules maximum with one AUC */
  45. #define AVA8_DEFAULT_MINER_CNT 4
  46. #define AVA8_DEFAULT_ASIC_MAX 26
  47. #define AVA8_DEFAULT_PLL_CNT 4
  48. #define AVA8_DEFAULT_PMU_CNT 2
  49. #define AVA8_DEFAULT_CORE_VOLT_CNT 8
  50. #define AVA8_DEFAULT_POLLING_DELAY 20 /* ms */
  51. #define AVA8_DEFAULT_NTIME_OFFSET 2
  52. #define AVA8_DEFAULT_SMARTSPEED_OFF 0
  53. #define AVA8_DEFAULT_SMARTSPEED_MODE1 1
  54. #define AVA8_DEFAULT_SMART_SPEED (AVA8_DEFAULT_SMARTSPEED_MODE1)
  55. #define AVA8_DEFAULT_TH_PASS 160
  56. #define AVA8_DEFAULT_TH_FAIL 8000
  57. #define AVA8_DEFAULT_TH_INIT 32767
  58. #define AVA8_DEFAULT_TH_ADD 1
  59. #define AVA8_DEFAULT_TH_MS 5
  60. #define AVA8_DEFAULT_TH_TIMEOUT 20000
  61. #define AVA8_DEFAULT_NONCE_MASK 24
  62. #define AVA8_DEFAULT_NONCE_CHECK 1
  63. #define AVA8_DEFAULT_MUX_L2H 0
  64. #define AVA8_DEFAULT_MUX_H2L 1
  65. #define AVA8_DEFAULT_H2LTIME0_SPD 3
  66. #define AVA8_DEFAULT_ROLL_ENABLE 1
  67. #define AVA8_DEFAULT_SPDLOW 0
  68. #define AVA8_DEFAULT_SPDHIGH 3
  69. #define AVA8_INVALID_TH_PASS -1
  70. #define AVA8_INVALID_TH_FAIL -1
  71. #define AVA8_INVALID_TH_TIMEOUT -1
  72. #define AVA8_INVALID_NONCE_MASK -1
  73. #define AVA8_INVALID_SPDLOW -1
  74. #define AVA851_DEFAULT_TH_PASS 200
  75. #define AVA851_DEFAULT_TH_FAIL 7000
  76. #define AVA851_DEFAULT_TH_TIMEOUT 16000
  77. #define AVA851_DEFAULT_SPDLOW 2
  78. #define AVA851_DEFAULT_NONCE_MASK 27
  79. #define AVA831_DEFAULT_TH_PASS 200
  80. #define AVA831_DEFAULT_TH_FAIL 7000
  81. #define AVA831_DEFAULT_TH_TIMEOUT 16000
  82. #define AVA831_DEFAULT_SPDLOW 2
  83. #define AVA831_DEFAULT_NONCE_MASK 27
  84. /* PID CONTROLLER*/
  85. #define AVA8_DEFAULT_PID_P 2
  86. #define AVA8_DEFAULT_PID_I 5
  87. #define AVA8_DEFAULT_PID_D 0
  88. #define AVA8_DEFAULT_PID_TEMP_MIN 50
  89. #define AVA8_DEFAULT_PID_TEMP_MAX 100
  90. #define AVA8_DEFAULT_IIC_DETECT false
  91. #define AVA8_PWM_MAX 0x3FF
  92. #define AVA8_DRV_DIFFMAX 2700
  93. #define AVA8_ASIC_TIMEOUT_CONST 419430400 /* (2^32 * 1000) / (256 * 40) */
  94. #define AVA8_MODULE_DETECT_INTERVAL 30 /* 30 s */
  95. #define AVA8_AUC_VER_LEN 12 /* Version length: 12 (AUC-YYYYMMDD) */
  96. #define AVA8_AUC_SPEED 400000
  97. #define AVA8_AUC_XDELAY 19200 /* 4800 = 1ms in AUC (11U14) */
  98. #define AVA8_AUC_P_SIZE 64
  99. #define AVA8_CONNECTER_AUC 1
  100. #define AVA8_CONNECTER_IIC 2
  101. /* avalon8 protocol package type from MM protocol.h */
  102. #define AVA8_MM_VER_LEN 15
  103. #define AVA8_MM_DNA_LEN 8
  104. #define AVA8_H1 'C'
  105. #define AVA8_H2 'N'
  106. #define AVA8_P_COINBASE_SIZE (6 * 1024 + 64)
  107. #define AVA8_P_MERKLES_COUNT 30
  108. #define AVA8_P_COUNT 40
  109. #define AVA8_P_DATA_LEN 32
  110. #define AVA8_OTP_LEN 32
  111. /* Broadcase with block iic_write*/
  112. #define AVA8_P_DETECT 0x10
  113. /* Broadcase With non-block iic_write*/
  114. #define AVA8_P_STATIC 0x11
  115. #define AVA8_P_JOB_ID 0x12
  116. #define AVA8_P_COINBASE 0x13
  117. #define AVA8_P_MERKLES 0x14
  118. #define AVA8_P_HEADER 0x15
  119. #define AVA8_P_TARGET 0x16
  120. #define AVA8_P_JOB_FIN 0x17
  121. /* Broadcase or with I2C address */
  122. #define AVA8_P_SET 0x20
  123. #define AVA8_P_SET_FIN 0x21
  124. #define AVA8_P_SET_VOLT 0x22
  125. #define AVA8_P_SET_PMU 0x24
  126. #define AVA8_P_SET_PLL 0x25
  127. #define AVA8_P_SET_SS 0x26
  128. /* 0x27 reserved */
  129. #define AVA8_P_SET_FAC 0x28
  130. #define AVA8_P_SET_OC 0x29
  131. /* Have to send with I2C address */
  132. #define AVA8_P_POLLING 0x30
  133. #define AVA8_P_SYNC 0x31
  134. #define AVA8_P_TEST 0x32
  135. #define AVA8_P_RSTMMTX 0x33
  136. #define AVA8_P_GET_VOLT 0x34
  137. /* Back to host */
  138. #define AVA8_P_ACKDETECT 0x40
  139. #define AVA8_P_STATUS 0x41
  140. #define AVA8_P_NONCE 0x42
  141. #define AVA8_P_TEST_RET 0x43
  142. #define AVA8_P_STATUS_VOLT 0x46
  143. #define AVA8_P_STATUS_PMU 0x48
  144. #define AVA8_P_STATUS_PLL 0x49
  145. #define AVA8_P_STATUS_LOG 0x4a
  146. #define AVA8_P_STATUS_ASIC 0x4b
  147. #define AVA8_P_STATUS_PVT 0x4c
  148. #define AVA8_P_STATUS_FAC 0x4d
  149. #define AVA8_P_STATUS_OC 0x4e
  150. #define AVA8_P_STATUS_OTP 0x4f
  151. #define AVA8_P_SET_ASIC_OTP 0x50
  152. #define AVA8_MODULE_BROADCAST 0
  153. /* End of avalon8 protocol package type */
  154. #define AVA8_IIC_RESET 0xa0
  155. #define AVA8_IIC_INIT 0xa1
  156. #define AVA8_IIC_DEINIT 0xa2
  157. #define AVA8_IIC_XFER 0xa5
  158. #define AVA8_IIC_INFO 0xa6
  159. #define AVA8_FREQ_INIT_MODE 0x0
  160. #define AVA8_FREQ_PLLADJ_MODE 0x1
  161. #define AVA8_DEFAULT_FACTORY_INFO_CNT (AVA8_DEFAULT_FACTORY_INFO_0_CNT + AVA8_DEFAULT_FACTORY_INFO_1_CNT)
  162. #define AVA8_DEFAULT_OVERCLOCKING_CNT 1
  163. #define AVA8_MM821_VIN_ADC_RATIO (3.3 / 4095.0 * 25.62 / 5.62 * 1000.0 * 100.0)
  164. #define AVA8_MM831_VIN_ADC_RATIO (3.3 / 4095.0 * 25.62 / 5.62 * 1000.0 * 100.0)
  165. #define AVA8_MM841_VIN_ADC_RATIO (3.3 / 4095.0 * 25.62 / 5.62 * 1000.0 * 100.0)
  166. #define AVA8_MM851_VIN_ADC_RATIO (3.3 / 4095.0 * 25.62 / 5.62 * 1000.0 * 100.0)
  167. #define AVA8_MM821_VOUT_ADC_RATIO (3.3 / 4095.0 * 63.0 / 20.0 * 10000.0 * 100.0)
  168. #define AVA8_MM831_VOUT_ADC_RATIO (3.3 / 4095.0 * 63.0 / 20.0 * 10000.0 * 100.0)
  169. #define AVA8_MM841_VOUT_ADC_RATIO (3.3 / 4095.0 * 63.0 / 20.0 * 10000.0 * 100.0)
  170. #define AVA8_MM851_VOUT_ADC_RATIO (3.3 / 4095.0 * 72.3 / 20.0 * 10000.0 * 100.0)
  171. #define AVA8_OTP_INDEX_READ_STEP 27
  172. #define AVA8_OTP_INDEX_ASIC_NUM 28
  173. #define AVA8_OTP_INDEX_CYCLE_HIT 29
  174. #define AVA8_OTP_INFO_LOTIDCRC_OFFSET 0
  175. #define AVA8_OTP_INFO_LOTID_OFFSET 6
  176. struct avalon8_pkg {
  177. uint8_t head[2];
  178. uint8_t type;
  179. uint8_t opt;
  180. uint8_t idx;
  181. uint8_t cnt;
  182. uint8_t data[32];
  183. uint8_t crc[2];
  184. };
  185. #define avalon8_ret avalon8_pkg
  186. struct avalon8_info {
  187. /* Public data */
  188. int64_t last_diff1;
  189. int64_t pending_diff1;
  190. double last_rej;
  191. int mm_count;
  192. int xfer_err_cnt;
  193. int pool_no;
  194. struct timeval firsthash;
  195. struct timeval last_fan_adj;
  196. struct timeval last_stratum;
  197. struct timeval last_detect;
  198. cglock_t update_lock;
  199. struct pool pool0;
  200. struct pool pool1;
  201. struct pool pool2;
  202. bool work_restart;
  203. uint32_t last_jobid;
  204. /* For connecter */
  205. char auc_version[AVA8_AUC_VER_LEN + 1];
  206. int auc_speed;
  207. int auc_xdelay;
  208. int auc_sensor;
  209. struct i2c_ctx *i2c_slaves[AVA8_DEFAULT_MODULARS];
  210. uint8_t connecter; /* AUC or IIC */
  211. /* For modulars */
  212. bool enable[AVA8_DEFAULT_MODULARS];
  213. bool reboot[AVA8_DEFAULT_MODULARS];
  214. struct timeval elapsed[AVA8_DEFAULT_MODULARS];
  215. uint8_t mm_dna[AVA8_DEFAULT_MODULARS][AVA8_MM_DNA_LEN];
  216. char mm_version[AVA8_DEFAULT_MODULARS][AVA8_MM_VER_LEN + 1]; /* It's a string */
  217. uint32_t total_asics[AVA8_DEFAULT_MODULARS];
  218. uint32_t max_ntime; /* Maximum: 7200 */
  219. uint8_t otp_info[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT][AVA8_OTP_LEN + 1];
  220. int mod_type[AVA8_DEFAULT_MODULARS];
  221. uint8_t miner_count[AVA8_DEFAULT_MODULARS];
  222. uint8_t asic_count[AVA8_DEFAULT_MODULARS];
  223. uint32_t freq_mode[AVA8_DEFAULT_MODULARS];
  224. int led_indicator[AVA8_DEFAULT_MODULARS];
  225. int fan_pct[AVA8_DEFAULT_MODULARS];
  226. int fan_cpm[AVA8_DEFAULT_MODULARS];
  227. int temp[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT][AVA8_DEFAULT_ASIC_MAX];
  228. int temp_mm[AVA8_DEFAULT_MODULARS];
  229. uint32_t core_volt[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT] \
  230. [AVA8_DEFAULT_ASIC_MAX][AVA8_DEFAULT_CORE_VOLT_CNT];
  231. uint8_t cutoff[AVA8_DEFAULT_MODULARS];
  232. int temp_target[AVA8_DEFAULT_MODULARS];
  233. int temp_overheat[AVA8_DEFAULT_MODULARS];
  234. /* pid controler*/
  235. int pid_p[AVA8_DEFAULT_MODULARS];
  236. int pid_i[AVA8_DEFAULT_MODULARS];
  237. int pid_d[AVA8_DEFAULT_MODULARS];
  238. double pid_u[AVA8_DEFAULT_MODULARS];
  239. int pid_e[AVA8_DEFAULT_MODULARS][3];
  240. int pid_0[AVA8_DEFAULT_MODULARS];
  241. int set_voltage_level[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT];
  242. uint32_t set_frequency[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT][AVA8_DEFAULT_PLL_CNT];
  243. uint32_t get_frequency[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT][AVA8_DEFAULT_ASIC_MAX][AVA8_DEFAULT_PLL_CNT];
  244. int set_asic_otp[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT];
  245. uint16_t get_vin[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT];
  246. uint32_t get_voltage[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT];
  247. uint32_t get_pll[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT][AVA8_DEFAULT_PLL_CNT];
  248. uint32_t get_asic[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT][AVA8_DEFAULT_ASIC_MAX][6];
  249. int8_t factory_info[AVA8_DEFAULT_FACTORY_INFO_CNT];
  250. int8_t overclocking_info[AVA8_DEFAULT_OVERCLOCKING_CNT];
  251. uint64_t local_works[AVA8_DEFAULT_MODULARS];
  252. uint64_t local_works_i[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT];
  253. uint64_t hw_works[AVA8_DEFAULT_MODULARS];
  254. uint64_t hw_works_i[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT];
  255. uint64_t chip_matching_work[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT][AVA8_DEFAULT_ASIC_MAX];
  256. uint32_t error_code[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT + 1];
  257. uint32_t error_crc[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_MINER_CNT];
  258. uint8_t error_polling_cnt[AVA8_DEFAULT_MODULARS];
  259. uint8_t power_good[AVA8_DEFAULT_MODULARS];
  260. char pmu_version[AVA8_DEFAULT_MODULARS][AVA8_DEFAULT_PMU_CNT][5];
  261. uint64_t diff1[AVA8_DEFAULT_MODULARS];
  262. uint16_t vin_adc_ratio[AVA8_DEFAULT_MODULARS];
  263. uint16_t vout_adc_ratio[AVA8_DEFAULT_MODULARS];
  264. bool conn_overloaded;
  265. };
  266. struct avalon8_iic_info {
  267. uint8_t iic_op;
  268. union {
  269. uint32_t aucParam[2];
  270. uint8_t slave_addr;
  271. } iic_param;
  272. };
  273. struct avalon8_dev_description {
  274. uint8_t dev_id_str[8];
  275. int mod_type;
  276. uint8_t miner_count; /* it should not greater than AVA8_DEFAULT_MINER_CNT */
  277. uint8_t asic_count; /* asic count each miner, it should not great than AVA8_DEFAULT_ASIC_MAX */
  278. uint16_t vin_adc_ratio;
  279. uint16_t vout_adc_ratio;
  280. int set_voltage_level;
  281. uint16_t set_freq[AVA8_DEFAULT_PLL_CNT];
  282. int set_asic_otp;
  283. };
  284. #define AVA8_WRITE_SIZE (sizeof(struct avalon8_pkg))
  285. #define AVA8_READ_SIZE AVA8_WRITE_SIZE
  286. #define AVA8_SEND_OK 0
  287. #define AVA8_SEND_ERROR -1
  288. extern char *set_avalon8_fan(char *arg);
  289. extern char *set_avalon8_freq(char *arg);
  290. extern char *set_avalon8_voltage_level(char *arg);
  291. extern char *set_avalon8_voltage_level_offset(char *arg);
  292. extern char *set_avalon8_asic_otp(char *arg);
  293. extern int opt_avalon8_temp_target;
  294. extern int opt_avalon8_polling_delay;
  295. extern int opt_avalon8_aucspeed;
  296. extern int opt_avalon8_aucxdelay;
  297. extern int opt_avalon8_smart_speed;
  298. extern bool opt_avalon8_iic_detect;
  299. extern int opt_avalon8_freq_sel;
  300. extern uint32_t opt_avalon8_th_pass;
  301. extern uint32_t opt_avalon8_th_fail;
  302. extern uint32_t opt_avalon8_th_init;
  303. extern uint32_t opt_avalon8_th_ms;
  304. extern uint32_t opt_avalon8_th_timeout;
  305. extern uint32_t opt_avalon8_th_add;
  306. extern uint32_t opt_avalon8_nonce_mask;
  307. extern uint32_t opt_avalon8_nonce_check;
  308. extern uint32_t opt_avalon8_mux_l2h;
  309. extern uint32_t opt_avalon8_mux_h2l;
  310. extern uint32_t opt_avalon8_h2ltime0_spd;
  311. extern uint32_t opt_avalon8_roll_enable;
  312. extern uint32_t opt_avalon8_spdlow;
  313. extern uint32_t opt_avalon8_spdhigh;
  314. extern uint32_t opt_avalon8_pid_p;
  315. extern uint32_t opt_avalon8_pid_i;
  316. extern uint32_t opt_avalon8_pid_d;
  317. #endif /* USE_AVALON8 */
  318. #endif /* _AVALON8_H_ */