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