Generalise more of libbitfury for more reuse in both nf1 and bxm drivers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
diff --git a/driver-bitfury.c b/driver-bitfury.c
index 7722132..fcca8dd 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -354,7 +354,7 @@ static bool nf1_reinit(struct cgpu_info *bitfury, struct bitfury_info *info)
spi_send_conf(info);
spi_send_init(info);
spi_reset(bitfury, info);
- return spi_txrx(bitfury, info);
+ return info->spi_txrx(bitfury, info);
}
static bool nf1_set_spi_settings(struct cgpu_info *bitfury, struct bitfury_info *info)
@@ -373,6 +373,7 @@ static bool nf1_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
bool ret = false;
int i, val;
+ info->spi_txrx = &mcp_spi_txrx;
mcp2210_get_gpio_settings(bitfury, mcp);
for (i = 0; i < 9; i++) {
@@ -510,43 +511,6 @@ static uint16_t calc_divisor(uint32_t system_clock, uint32_t freq)
return divisor;
}
-static bool bxm_spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info)
-{
- int err, amount, len;
- uint16_t length;
- char buf[1024];
-
- len = info->spibufsz;
- length = info->spibufsz - 1; //FTDI length is shifted by one 0x0000 = one byte
- buf[0] = READ_WRITE_BYTES_SPI0;
- buf[1] = length & 0x00FF;
- buf[2] = (length & 0xFF00) >> 8;
- memcpy(&buf[3], info->spibuf, info->spibufsz);
- info->spibufsz += 3;
- err = usb_write(bitfury, buf, info->spibufsz, &amount, C_BXM_SPITX);
- if (err || amount != (int)info->spibufsz) {
- applog(LOG_ERR, "%s %d: SPI TX error %d, sent %d of %d", bitfury->drv->name,
- bitfury->device_id, err, amount, info->spibufsz);
- return false;
- }
- info->spibufsz = len;
- /* We shouldn't even get a timeout error on reads in spi mode */
- err = usb_read(bitfury, info->spibuf, len, &amount, C_BXM_SPIRX);
- if (err || amount != len) {
- applog(LOG_ERR, "%s %d: SPI RX error %d, read %d of %d", bitfury->drv->name,
- bitfury->device_id, err, amount, info->spibufsz);
- return false;
- }
- amount = usb_buffer_size(bitfury);
- if (amount) {
- applog(LOG_ERR, "%s %d: SPI RX Extra read buffer size %d", bitfury->drv->name,
- bitfury->device_id, amount);
- usb_buffer_clear(bitfury);
- return false;
- }
- return true;
-}
-
static void bxm_close(struct cgpu_info *bitfury)
{
unsigned char bitmask = 0;
@@ -699,13 +663,14 @@ static bool bxm_reinit(struct cgpu_info *bitfury, struct bitfury_info *info)
spi_set_freq(info);
spi_send_conf(info);
spi_send_init(info);
- return bxm_spi_txrx(bitfury, info);
+ return info->spi_txrx(bitfury, info);
}
static bool bxm_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
{
bool ret;
+ info->spi_txrx = &ftdi_spi_txrx;
ret = bxm_open(bitfury);
if (!ret)
goto out;
@@ -722,7 +687,7 @@ static bool bxm_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
/* Do a dummy read */
memset(info->spibuf, 0, 80);
info->spibufsz = 80;
- ret = bxm_spi_txrx(bitfury, info);
+ ret = info->spi_txrx(bitfury, info);
if (!ret)
goto out;
/* FIXME make configurable and use a faster default. */
@@ -1250,54 +1215,6 @@ static int64_t nf1_scan(struct thr_info *thr, struct cgpu_info *bitfury,
return ret;
}
-static bool bxm_sendHashData(struct thr_info *thr, struct cgpu_info *bitfury,
- struct bitfury_info *info)
-{
- unsigned *newbuf = info->newbuf;
- unsigned *oldbuf = info->oldbuf;
- struct bitfury_payload *p = &(info->payload);
- struct bitfury_payload *op = &(info->opayload);
- unsigned int localvec[20];
-
- /* Programming next value */
- memcpy(localvec, p, 20 * 4);
- ms3steps(localvec);
-
- spi_clear_buf(info);
- spi_add_break(info);
- spi_add_data(info, 0x3000, (void*)localvec, 19 * 4);
- if (!bxm_spi_txrx(bitfury, info))
- return false;
-
- memcpy(newbuf, info->spibuf + 4, 17 * 4);
-
- info->job_switched = newbuf[16] != oldbuf[16];
-
- if (likely(info->second_run)) {
- if (info->job_switched) {
- int i;
-
- for (i = 0; i < 16; i++) {
- if (oldbuf[i] != newbuf[i] && info->owork) {
- uint32_t nonce; //possible nonce
-
- nonce = decnonce(newbuf[i]);
- if (bitfury_checkresults(thr, info->owork, nonce))
- info->nonces++;
- }
- }
-
- memcpy(op, p, sizeof(struct bitfury_payload));
- memcpy(oldbuf, newbuf, 17 * 4);
- }
- } else
- info->second_run = true;
-
- cgsleep_ms(BITFURY_REFRESH_DELAY);
-
- return true;
-}
-
static int64_t bxm_scan(struct thr_info *thr, struct cgpu_info *bitfury,
struct bitfury_info *info)
{
@@ -1312,7 +1229,7 @@ static int64_t bxm_scan(struct thr_info *thr, struct cgpu_info *bitfury,
}
bitfury_work_to_payload(&info->payload, info->work);
}
- if (!bxm_sendHashData(thr, bitfury, info))
+ if (!libbitfury_sendHashData(thr, bitfury, info))
return -1;
if (info->job_switched) {
diff --git a/driver-bitfury.h b/driver-bitfury.h
index f7af174..fb66114 100644
--- a/driver-bitfury.h
+++ b/driver-bitfury.h
@@ -104,6 +104,8 @@ struct bitfury_info {
bool second_run;
struct work *work;
struct work *owork;
+
+ bool (*spi_txrx)(struct cgpu_info *, struct bitfury_info *info);
};
#endif /* BITFURY_H */
diff --git a/libbitfury.c b/libbitfury.c
index bd10ff0..4cc7172 100644
--- a/libbitfury.c
+++ b/libbitfury.c
@@ -242,7 +242,7 @@ bool spi_reset(struct cgpu_info *bitfury, struct bitfury_info *info)
return true;
}
-bool spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info)
+bool mcp_spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info)
{
unsigned int length, sendrcv;
int offset = 0;
@@ -272,6 +272,45 @@ bool spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info)
return true;
}
+#define READ_WRITE_BYTES_SPI0 0x31
+
+bool ftdi_spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info)
+{
+ int err, amount, len;
+ uint16_t length;
+ char buf[1024];
+
+ len = info->spibufsz;
+ length = info->spibufsz - 1; //FTDI length is shifted by one 0x0000 = one byte
+ buf[0] = READ_WRITE_BYTES_SPI0;
+ buf[1] = length & 0x00FF;
+ buf[2] = (length & 0xFF00) >> 8;
+ memcpy(&buf[3], info->spibuf, info->spibufsz);
+ info->spibufsz += 3;
+ err = usb_write(bitfury, buf, info->spibufsz, &amount, C_BXM_SPITX);
+ if (err || amount != (int)info->spibufsz) {
+ applog(LOG_ERR, "%s %d: SPI TX error %d, sent %d of %d", bitfury->drv->name,
+ bitfury->device_id, err, amount, info->spibufsz);
+ return false;
+ }
+ info->spibufsz = len;
+ /* We shouldn't even get a timeout error on reads in spi mode */
+ err = usb_read(bitfury, info->spibuf, len, &amount, C_BXM_SPIRX);
+ if (err || amount != len) {
+ applog(LOG_ERR, "%s %d: SPI RX error %d, read %d of %d", bitfury->drv->name,
+ bitfury->device_id, err, amount, info->spibufsz);
+ return false;
+ }
+ amount = usb_buffer_size(bitfury);
+ if (amount) {
+ applog(LOG_ERR, "%s %d: SPI RX Extra read buffer size %d", bitfury->drv->name,
+ bitfury->device_id, amount);
+ usb_buffer_clear(bitfury);
+ return false;
+ }
+ return true;
+}
+
#define BT_OFFSETS 3
bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32_t nonce)
@@ -306,7 +345,7 @@ bool libbitfury_sendHashData(struct thr_info *thr, struct cgpu_info *bitfury,
spi_clear_buf(info);
spi_add_break(info);
spi_add_data(info, 0x3000, (void*)localvec, 19 * 4);
- if (!spi_txrx(bitfury, info))
+ if (!info->spi_txrx(bitfury, info))
return false;
memcpy(newbuf, info->spibuf + 4, 17 * 4);
diff --git a/libbitfury.h b/libbitfury.h
index 79ed564..90a79c5 100644
--- a/libbitfury.h
+++ b/libbitfury.h
@@ -23,7 +23,8 @@ void spi_add_buf(struct bitfury_info *info, const void *buf, const int sz);
void spi_add_break(struct bitfury_info *info);
void spi_add_data(struct bitfury_info *info, uint16_t addr, const void *buf, int len);
bool spi_reset(struct cgpu_info *bitfury, struct bitfury_info *info);
-bool spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info);
+bool mcp_spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info);
+bool ftdi_spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info);
bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32_t nonce);
bool libbitfury_sendHashData(struct thr_info *thr, struct cgpu_info *bitfury,
struct bitfury_info *info);