Browse Source

bitfury: Move i2c slot handling to metabank-specific driver code

Luke Dashjr 12 years ago
parent
commit
f20a440604
5 changed files with 85 additions and 69 deletions
  1. 81 1
      driver-bitfury.c
  2. 0 64
      libbitfury.c
  3. 1 2
      libbitfury.h
  4. 1 2
      spidevc.c
  5. 2 0
      spidevc.h

+ 81 - 1
driver-bitfury.c

@@ -27,11 +27,91 @@
 #include "libbitfury.h"
 #include "libbitfury.h"
 #include "util.h"
 #include "util.h"
 #include "spidevc.h"
 #include "spidevc.h"
+#include "tm_i2c.h"
 
 
 #define GOLDEN_BACKLOG 5
 #define GOLDEN_BACKLOG 5
 
 
 struct device_drv bitfury_drv;
 struct device_drv bitfury_drv;
 
 
+static
+bool metabank_spi_txrx(struct spi_port *port)
+{
+	struct cgpu_info * const proc = port->cgpu;
+	struct bitfury_device * const bitfury = proc->device_data;
+	tm_i2c_set_oe(bitfury->slot);
+	const bool rv = sys_spi_txrx(port);
+	tm_i2c_clear_oe(bitfury->slot);
+	return rv;
+}
+
+static
+int libbitfury_detectChips(struct bitfury_device *devices) {
+	struct spi_port *port;
+	int n = 0;
+	int i, j;
+	static bool slot_on[32];
+	struct timespec t1, t2;
+	struct bitfury_device dummy_bitfury;
+	struct cgpu_info dummy_cgpu;
+
+	if (tm_i2c_init() < 0) {
+		printf("I2C init error\n");
+		return(1);
+	}
+
+
+	dummy_cgpu.device_data = &dummy_bitfury;
+	
+	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
+	for (i = 0; i < 32; i++) {
+		int slot_detected = tm_i2c_detect(i) != -1;
+		slot_on[i] = slot_detected;
+		tm_i2c_clear_oe(i);
+		cgsleep_ms(1);
+	}
+
+	for (i = 0; i < 32; i++) {
+		if (slot_on[i]) {
+			int chip_n;
+			
+			port = malloc(sizeof(*port));
+			*port = *sys_spi;
+			port->cgpu = &dummy_cgpu;
+			port->txrx = metabank_spi_txrx;
+			dummy_bitfury.slot = i;
+			
+			chip_n = libbitfury_detectChips1(port);
+			if (chip_n)
+			{
+				applog(LOG_WARNING, "BITFURY slot %d: %d chips detected", i, chip_n);
+				for (j = 0; j < chip_n; ++j)
+				{
+					devices[n].spi = port;
+					devices[n].slot = i;
+					devices[n].fasync = j;
+					n++;
+				}
+			}
+			else
+				free(port);
+		}
+	}
+
+	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);
+
+	return n; //!!!
+	//return 1;
+}
+
+void libbitfury_shutdownChips(struct bitfury_device *devices, int chip_n) {
+	int i;
+	for (i = 0; i < chip_n; i++) {
+		send_shutdown(devices[i].spi, devices[i].slot, devices[i].fasync);
+	}
+	tm_i2c_close();
+}
+
+
 // Forward declarations
 // Forward declarations
 static bool bitfury_prepare(struct thr_info *thr);
 static bool bitfury_prepare(struct thr_info *thr);
 int calc_stat(time_t * stat_ts, time_t stat, struct timeval now);
 int calc_stat(time_t * stat_ts, time_t stat, struct timeval now);
@@ -50,7 +130,7 @@ static void bitfury_detect(void)
 	spi_init();
 	spi_init();
 	if (!sys_spi)
 	if (!sys_spi)
 		return;
 		return;
-	chip_n = libbitfury_detectChips(sys_spi, bitfury_info->devices);
+	chip_n = libbitfury_detectChips(bitfury_info->devices);
 	if (!chip_n) {
 	if (!chip_n) {
 		applog(LOG_WARNING, "No Bitfury chips detected!");
 		applog(LOG_WARNING, "No Bitfury chips detected!");
 		return;
 		return;

+ 0 - 64
libbitfury.c

@@ -28,7 +28,6 @@
 #include <string.h>
 #include <string.h>
 
 
 #include "miner.h"
 #include "miner.h"
-#include "tm_i2c.h"
 #include "libbitfury.h"
 #include "libbitfury.h"
 
 
 #include "spidevc.h"
 #include "spidevc.h"
@@ -219,9 +218,7 @@ void send_reinit(struct spi_port *port, int slot, int chip_n, int n) {
 	set_freq(port, n);
 	set_freq(port, n);
 	send_conf(port);
 	send_conf(port);
 	send_init(port);
 	send_init(port);
-	tm_i2c_set_oe(slot);
 	spi_txrx(port);
 	spi_txrx(port);
-	tm_i2c_clear_oe(slot);
 }
 }
 
 
 void send_shutdown(struct spi_port *port, int slot, int chip_n) {
 void send_shutdown(struct spi_port *port, int slot, int chip_n) {
@@ -229,9 +226,7 @@ void send_shutdown(struct spi_port *port, int slot, int chip_n) {
 	spi_emit_break(port);
 	spi_emit_break(port);
 	spi_emit_fasync(port, chip_n);
 	spi_emit_fasync(port, chip_n);
 	config_reg(port, 4, 0); /* Disable slow oscillator */
 	config_reg(port, 4, 0); /* Disable slow oscillator */
-	tm_i2c_set_oe(slot);
 	spi_txrx(port);
 	spi_txrx(port);
-	tm_i2c_clear_oe(slot);
 }
 }
 
 
 void send_freq(struct spi_port *port, int slot, int chip_n, int bits) {
 void send_freq(struct spi_port *port, int slot, int chip_n, int bits) {
@@ -239,9 +234,7 @@ void send_freq(struct spi_port *port, int slot, int chip_n, int bits) {
 	spi_emit_break(port);
 	spi_emit_break(port);
 	spi_emit_fasync(port, chip_n);
 	spi_emit_fasync(port, chip_n);
 	set_freq(port, bits);
 	set_freq(port, bits);
-	tm_i2c_set_oe(slot);
 	spi_txrx(port);
 	spi_txrx(port);
-	tm_i2c_clear_oe(slot);
 }
 }
 
 
 unsigned int c_diff(unsigned ocounter, unsigned counter) {
 unsigned int c_diff(unsigned ocounter, unsigned counter) {
@@ -322,59 +315,6 @@ int libbitfury_detectChips1(struct spi_port *port) {
 	return n;
 	return n;
 }
 }
 
 
-int libbitfury_detectChips(struct spi_port *port, struct bitfury_device *devices) {
-	int n = 0;
-	int i, j;
-	static bool slot_on[32];
-	struct timespec t1, t2;
-
-	if (tm_i2c_init() < 0) {
-		printf("I2C init error\n");
-		return(1);
-	}
-
-
-	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
-	for (i = 0; i < 32; i++) {
-		int slot_detected = tm_i2c_detect(i) != -1;
-		slot_on[i] = slot_detected;
-		tm_i2c_clear_oe(i);
-		cgsleep_ms(1);
-	}
-
-	for (i = 0; i < 32; i++) {
-		if (slot_on[i]) {
-			int chip_n;
-			tm_i2c_set_oe(i);
-			chip_n = libbitfury_detectChips1(port);
-			tm_i2c_clear_oe(i);
-			if (chip_n)
-			{
-				applog(LOG_WARNING, "BITFURY slot %d: %d chips detected", i, chip_n);
-				for (j = 0; j < chip_n; ++j)
-				{
-					devices[n].slot = i;
-					devices[n].fasync = j;
-					n++;
-				}
-			}
-		}
-	}
-
-	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);
-
-	return n; //!!!
-	//return 1;
-}
-
-void libbitfury_shutdownChips(struct bitfury_device *devices, int chip_n) {
-	int i;
-	for (i = 0; i < chip_n; i++) {
-		send_shutdown(devices[i].spi, devices[i].slot, devices[i].fasync);
-	}
-	tm_i2c_close();
-}
-
 unsigned decnonce(unsigned in)
 unsigned decnonce(unsigned in)
 {
 {
 	unsigned out;
 	unsigned out;
@@ -488,11 +428,9 @@ void libbitfury_sendHashData1(int chip_id, struct bitfury_device *d, bool second
 		if (smart) {
 		if (smart) {
 			config_reg(port, 3, 0);
 			config_reg(port, 3, 0);
 		}
 		}
-		tm_i2c_set_oe(slot);
 		clock_gettime(CLOCK_REALTIME, &(time));
 		clock_gettime(CLOCK_REALTIME, &(time));
 		d_time = t_diff(time, d->predict1);
 		d_time = t_diff(time, d->predict1);
 		spi_txrx(port);
 		spi_txrx(port);
-		tm_i2c_clear_oe(slot);
 		memcpy(newbuf, spi_getrxbuf(port)+4 + chip, 17*4);
 		memcpy(newbuf, spi_getrxbuf(port)+4 + chip, 17*4);
 
 
 		d->job_switched = newbuf[16] != oldbuf[16];
 		d->job_switched = newbuf[16] != oldbuf[16];
@@ -660,9 +598,7 @@ void libbitfury_sendHashData1(int chip_id, struct bitfury_device *d, bool second
 			if (smart) {
 			if (smart) {
 				config_reg(port, 3, 1);
 				config_reg(port, 3, 1);
 			}
 			}
-			tm_i2c_set_oe(slot);
 			spi_txrx(port);
 			spi_txrx(port);
-			tm_i2c_clear_oe(slot);
 			memcpy(newbuf, spi_getrxbuf(port)+4 + chip, 17*4);
 			memcpy(newbuf, spi_getrxbuf(port)+4 + chip, 17*4);
 			d->counter2 = get_counter(newbuf, oldbuf);
 			d->counter2 = get_counter(newbuf, oldbuf);
 
 

+ 1 - 2
libbitfury.h

@@ -57,9 +57,8 @@ void libbitfury_sendHashData(struct bitfury_device *bf, int chip_n);
 void work_to_payload(struct bitfury_payload *p, struct work *w);
 void work_to_payload(struct bitfury_payload *p, struct work *w);
 struct timespec t_diff(struct timespec start, struct timespec end);
 struct timespec t_diff(struct timespec start, struct timespec end);
 extern void send_reinit(struct spi_port *, int slot, int chip_n, int n);
 extern void send_reinit(struct spi_port *, int slot, int chip_n, int n);
+extern void send_shutdown(struct spi_port *, int slot, int chip_n);
 extern void send_freq(struct spi_port *, int slot, int chip_n, int bits);
 extern void send_freq(struct spi_port *, int slot, int chip_n, int bits);
 extern int libbitfury_detectChips1(struct spi_port *);
 extern int libbitfury_detectChips1(struct spi_port *);
-extern int libbitfury_detectChips(struct spi_port *, struct bitfury_device *devices);
-void libbitfury_shutdownChips(struct bitfury_device *devices, int chip_n);
 
 
 #endif /* __LIBBITFURY_H__ */
 #endif /* __LIBBITFURY_H__ */

+ 1 - 2
spidevc.c

@@ -48,7 +48,7 @@
 #define HAVE_LINUX_SPI
 #define HAVE_LINUX_SPI
 
 
 #ifdef HAVE_LINUX_SPI
 #ifdef HAVE_LINUX_SPI
-static bool sys_spi_txrx(struct spi_port *port);
+bool sys_spi_txrx(struct spi_port *port);
 static volatile unsigned *gpio;
 static volatile unsigned *gpio;
 #endif
 #endif
 
 
@@ -123,7 +123,6 @@ void spi_reset(void)
 	return false;  \
 	return false;  \
 }while(0)
 }while(0)
 
 
-static
 bool sys_spi_txrx(struct spi_port *port)
 bool sys_spi_txrx(struct spi_port *port)
 {
 {
 	const void *wrbuf = spi_gettxbuf(port);
 	const void *wrbuf = spi_gettxbuf(port);

+ 2 - 0
spidevc.h

@@ -69,4 +69,6 @@ bool spi_txrx(struct spi_port *port)
 	return port->txrx(port);
 	return port->txrx(port);
 }
 }
 
 
+extern bool sys_spi_txrx(struct spi_port *);
+
 #endif
 #endif