Commit 22a855c1a946a29c6f70d78fc6cf238911061147

Con Kolivas 2014-01-09T16:06:46

Set the mcp2210 transfer setting only when it changes.

diff --git a/driver-bitfury.c b/driver-bitfury.c
index e5f7a0e..3bad374 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -377,7 +377,7 @@ static bool nf1_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
 
 	buf[0] = 0;
 	length = 1;
-	if (!mcp2210_spi_transfer(bitfury, buf, &length))
+	if (!mcp2210_spi_transfer(bitfury, mcp, buf, &length))
 		goto out;
 	/* after this command SCK_OVRRIDE should read the same as current SCK
 	 * value (which for mode 0 should be 0) */
@@ -392,7 +392,7 @@ static bool nf1_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
 		goto out;
 	buf[0] = 0;
 	length = 1;
-	if (!mcp2210_spi_transfer(bitfury, buf, &length))
+	if (!mcp2210_spi_transfer(bitfury, mcp, buf, &length))
 		goto out;
 	/* after this command SCK_OVRRIDE should read the same as current SCK
 	 * value (which for mode 2 should be 1) */
@@ -407,7 +407,7 @@ static bool nf1_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
 		goto out;
 	buf[0] = 0;
 	length = 1;
-	if (!mcp2210_spi_transfer(bitfury, buf, &length))
+	if (!mcp2210_spi_transfer(bitfury, mcp, buf, &length))
 		goto out;
 	if (!mcp2210_get_gpio_pinval(bitfury, NF1_PIN_SCK_OVR, &val))
 		goto out;
diff --git a/driver-bitfury.h b/driver-bitfury.h
index cc20591..30d7e4d 100644
--- a/driver-bitfury.h
+++ b/driver-bitfury.h
@@ -87,7 +87,6 @@ struct bitfury_info {
 	unsigned oldbuf[17];
 	int job_switched;
 	unsigned int results[16];
-	int results_n;
 	struct work *work;
 	struct work *owork;
 };
diff --git a/libbitfury.c b/libbitfury.c
index e32828b..17f2cc1 100644
--- a/libbitfury.c
+++ b/libbitfury.c
@@ -230,7 +230,7 @@ bool spi_reset(struct cgpu_info *bitfury, struct bitfury_info *info)
 		char buf[1] = {0x81}; // will send this waveform: - _ _ _ _ _ _ -
 		unsigned int length = 1;
 
-		if (!mcp2210_spi_transfer(bitfury, buf, &length))
+		if (!mcp2210_spi_transfer(bitfury, &info->mcp, buf, &length))
 			return false;
 	}
 
@@ -252,7 +252,7 @@ bool spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info)
 	       bitfury->device_id, length);
 	while (length > MCP2210_TRANSFER_MAX) {
 		sendrcv = MCP2210_TRANSFER_MAX;
-		if (!mcp2210_spi_transfer(bitfury, info->spibuf + offset, &sendrcv))
+		if (!mcp2210_spi_transfer(bitfury, &info->mcp, info->spibuf + offset, &sendrcv))
 			return false;
 		if (sendrcv != MCP2210_TRANSFER_MAX) {
 			applog(LOG_DEBUG, "%s %d: Send/Receive size mismatch sent %d received %d",
@@ -262,7 +262,7 @@ bool spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info)
 		offset += MCP2210_TRANSFER_MAX;
 	}
 	sendrcv = length;
-	if (!mcp2210_spi_transfer(bitfury, info->spibuf + offset, &sendrcv))
+	if (!mcp2210_spi_transfer(bitfury, &info->mcp, info->spibuf + offset, &sendrcv))
 		return false;
 	if (sendrcv != length) {
 		applog(LOG_WARNING, "%s %d: Send/Receive size mismatch sent %d received %d",
diff --git a/mcp2210.c b/mcp2210.c
index 2906651..83ff95f 100644
--- a/mcp2210.c
+++ b/mcp2210.c
@@ -299,10 +299,10 @@ mcp2210_set_spi_transfer_settings(struct cgpu_info *cgpu, unsigned int bitrate, 
 
 /* Perform an spi transfer of *length bytes and return the amount of data
  * returned in the same buffer in *length */
-bool mcp2210_spi_transfer(struct cgpu_info *cgpu, char *data, unsigned int *length)
+bool mcp2210_spi_transfer(struct cgpu_info *cgpu, struct mcp_settings *mcp,
+			  char *data, unsigned int *length)
 {
-	unsigned int bitrate, icsv, acsv, cstdd, ldbtcsd, sdbd, bpst, spimode;
-	uint8_t res, status, orig_len, len, offset = 0;
+	uint8_t res, status, orig_len, offset = 0;
 	char buf[MCP2210_BUFFER_LENGTH];
 
 	if (unlikely(*length > MCP2210_TRANSFER_MAX || !*length)) {
@@ -310,14 +310,14 @@ bool mcp2210_spi_transfer(struct cgpu_info *cgpu, char *data, unsigned int *leng
 		       cgpu->device_id, *length);
 		return false;
 	}
-	if (!mcp2210_get_spi_transfer_settings(cgpu, &bitrate, &icsv, &acsv, &cstdd,
-	    &ldbtcsd, &sdbd, &bpst, &spimode))
-		return false;
-	bpst = *length;
-	if (!mcp2210_set_spi_transfer_settings(cgpu, bitrate, icsv, acsv, cstdd,
-	    ldbtcsd, sdbd, bpst, spimode))
-		return false;
-	orig_len = len = *length;
+	if (mcp->bpst != *length) {
+		/* Set the transfer setting only when it changes. */
+		mcp->bpst = *length;
+		if (!mcp2210_set_spi_transfer_settings(cgpu, mcp->bitrate, mcp->icsv,
+		    mcp->acsv, mcp->cstdd, mcp->ldbtcsd, mcp->sdbd, mcp->bpst, mcp->spimode))
+			return false;
+	}
+	orig_len = *length;
 retry:
 	applog(LOG_DEBUG, "%s %d: SPI sending %u bytes", cgpu->drv->name, cgpu->device_id,
 	       *length);
@@ -333,7 +333,6 @@ retry:
 	res = (uint8_t)buf[1];
 	switch(res) {
 		case MCP2210_SPI_TRANSFER_SUCCESS:
-			len -= *length;
 			*length = buf[2];
 			status = buf[3];
 			applog(LOG_DEBUG, "%s %d: SPI transfer success, received %u bytes status 0x%x",
diff --git a/mcp2210.h b/mcp2210.h
index 2b9c3d1..cb75443 100644
--- a/mcp2210.h
+++ b/mcp2210.h
@@ -67,6 +67,7 @@ bool
 mcp2210_set_spi_transfer_settings(struct cgpu_info *cgpu, unsigned int bitrate, unsigned int icsv,
 				  unsigned int acsv, unsigned int cstdd, unsigned int ldbtcsd,
 				  unsigned int sdbd, unsigned int bpst, unsigned int spimode);
-bool mcp2210_spi_transfer(struct cgpu_info *cgpu, char *data, unsigned int *length);
+bool mcp2210_spi_transfer(struct cgpu_info *cgpu, struct mcp_settings *mcp,
+			  char *data, unsigned int *length);
 
 #endif /* MCP2210_H */