Factor out SPI write/read chunking wrappers

Corresponding to flashrom svn r1298.

Signed-off-by: Michael Karcher <flashrom@mkarcher.dialup.fu-berlin.de>
Acked-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
diff --git a/bitbang_spi.c b/bitbang_spi.c
index fb404ac..d63c769 100644
--- a/bitbang_spi.c
+++ b/bitbang_spi.c
@@ -151,14 +151,3 @@
 
 	return 0;
 }
-
-int bitbang_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
-{
-	/* Maximum read length is unlimited, use 64k bytes. */
-	return spi_read_chunked(flash, buf, start, len, 64 * 1024);
-}
-
-int bitbang_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
-{
-	return spi_write_chunked(flash, buf, start, len, 256);
-}
diff --git a/buspirate_spi.c b/buspirate_spi.c
index d24f890..e7a7a10 100644
--- a/buspirate_spi.c
+++ b/buspirate_spi.c
@@ -326,13 +326,3 @@
 
 	return ret;
 }
-
-int buspirate_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
-{
-	return spi_read_chunked(flash, buf, start, len, 12);
-}
-
-int buspirate_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
-{
-	return spi_write_chunked(flash, buf, start, len, 12);
-}
diff --git a/dummyflasher.c b/dummyflasher.c
index d818a84..b6e67db 100644
--- a/dummyflasher.c
+++ b/dummyflasher.c
@@ -507,12 +507,6 @@
 	return 0;
 }
 
-int dummy_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
-{
-	/* Maximum read length is unlimited, use 64kB. */
-	return spi_read_chunked(flash, buf, start, len, 64 * 1024);
-}
-
 int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
 {
 	return spi_write_chunked(flash, buf, start, len,
diff --git a/ft2232_spi.c b/ft2232_spi.c
index 7373624..7ff1d81 100644
--- a/ft2232_spi.c
+++ b/ft2232_spi.c
@@ -352,17 +352,6 @@
 	return failed ? -1 : 0;
 }
 
-int ft2232_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
-{
-	/* Maximum read length is 64k bytes. */
-	return spi_read_chunked(flash, buf, start, len, 64 * 1024);
-}
-
-int ft2232_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
-{
-	return spi_write_chunked(flash, buf, start, len, 256);
-}
-
 void print_supported_usbdevs(const struct usbdev_status *devs)
 {
 	int i;
diff --git a/ichspi.c b/ichspi.c
index f2814d1..4315e78 100644
--- a/ichspi.c
+++ b/ichspi.c
@@ -834,26 +834,6 @@
 	return -1;
 }
 
-int ich_spi_read(struct flashchip *flash, uint8_t * buf, int start, int len)
-{
-	int maxdata = 64;
-
-	if (spi_controller == SPI_CONTROLLER_VIA)
-		maxdata = 16;
-
-	return spi_read_chunked(flash, buf, start, len, maxdata);
-}
-
-int ich_spi_write_256(struct flashchip *flash, uint8_t * buf, int start, int len)
-{
-	int maxdata = 64;
-
-	if (spi_controller == SPI_CONTROLLER_VIA)
-		maxdata = 16;
-
-	return spi_write_chunked(flash, buf, start, len, maxdata);
-}
-
 int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
 		    const unsigned char *writearr, unsigned char *readarr)
 {
diff --git a/it85spi.c b/it85spi.c
index 1710d8e..a388d10 100644
--- a/it85spi.c
+++ b/it85spi.c
@@ -347,14 +347,4 @@
 	return 0;
 }
 
-int it85_spi_read(struct flashchip *flash, uint8_t * buf, int start, int len)
-{
-	return spi_read_chunked(flash, buf, start, len, 64);
-}
-
-int it85_spi_write_256(struct flashchip *flash, uint8_t * buf, int start, int len)
-{
-	return spi_write_chunked(flash, buf, start, len, 64);
-}
-
 #endif
diff --git a/programmer.h b/programmer.h
index 95d1fae..6ade8ed 100644
--- a/programmer.h
+++ b/programmer.h
@@ -374,7 +374,6 @@
 void dummy_chip_readn(uint8_t *buf, const chipaddr addr, size_t len);
 int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
 		      const unsigned char *writearr, unsigned char *readarr);
-int dummy_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len);
 int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len);
 #endif
 
@@ -487,8 +486,6 @@
 };
 int ft2232_spi_init(void);
 int ft2232_spi_send_command(unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr);
-int ft2232_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len);
-int ft2232_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len);
 extern const struct usbdev_status devs_ft2232spi[];
 void print_supported_usbdevs(const struct usbdev_status *devs);
 #endif
@@ -509,8 +506,6 @@
 int bitbang_spi_init(const struct bitbang_spi_master *master, int halfperiod);
 int bitbang_spi_shutdown(const struct bitbang_spi_master *master);
 int bitbang_spi_send_command(unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr);
-int bitbang_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len);
-int bitbang_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len);
 
 /* buspirate_spi.c */
 struct buspirate_spispeeds {
@@ -520,8 +515,6 @@
 int buspirate_spi_init(void);
 int buspirate_spi_shutdown(void);
 int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr);
-int buspirate_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len);
-int buspirate_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len);
 
 /* dediprog.c */
 int dediprog_init(void);
@@ -586,7 +579,13 @@
 	SPI_CONTROLLER_INVALID /* This must always be the last entry. */
 };
 extern const int spi_programmer_count;
+
+#define MAX_DATA_UNSPECIFIED 0
+#define MAX_DATA_READ_UNLIMITED 64 * 1024
+#define MAX_DATA_WRITE_UNLIMITED 256
 struct spi_programmer {
+	int max_data_read;
+	int max_data_write;
 	int (*command)(unsigned int writecnt, unsigned int readcnt,
 		   const unsigned char *writearr, unsigned char *readarr);
 	int (*multicommand)(struct spi_command *cmds);
@@ -601,6 +600,8 @@
 int default_spi_send_command(unsigned int writecnt, unsigned int readcnt,
 			     const unsigned char *writearr, unsigned char *readarr);
 int default_spi_send_multicommand(struct spi_command *cmds);
+int default_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len);
+int default_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len);
 
 /* ichspi.c */
 #if CONFIG_INTERNAL == 1
@@ -610,8 +611,6 @@
 int via_init_spi(struct pci_dev *dev);
 int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
 		    const unsigned char *writearr, unsigned char *readarr);
-int ich_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len);
-int ich_spi_write_256(struct flashchip *flash, uint8_t * buf, int start, int len);
 int ich_spi_send_multicommand(struct spi_command *cmds);
 #endif
 
@@ -620,8 +619,6 @@
 int it85xx_shutdown(void);
 int it85xx_spi_send_command(unsigned int writecnt, unsigned int readcnt,
 			const unsigned char *writearr, unsigned char *readarr);
-int it85_spi_read(struct flashchip *flash, uint8_t * buf, int start, int len);
-int it85_spi_write_256(struct flashchip *flash, uint8_t * buf, int start, int len);
 
 /* it87spi.c */
 void enter_conf_mode_ite(uint16_t port);
@@ -638,8 +635,6 @@
 int sb600_probe_spi(struct pci_dev *dev);
 int sb600_spi_send_command(unsigned int writecnt, unsigned int readcnt,
 		      const unsigned char *writearr, unsigned char *readarr);
-int sb600_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len);
-int sb600_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len);
 #endif
 
 /* wbsio_spi.c */
diff --git a/sb600spi.c b/sb600spi.c
index 845cf58..269af8a 100644
--- a/sb600spi.c
+++ b/sb600spi.c
@@ -43,17 +43,6 @@
 
 static uint8_t *sb600_spibar = NULL;
 
-int sb600_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
-{
-	/* Maximum read length is 8 bytes. */
-	return spi_read_chunked(flash, buf, start, len, 8);
-}
-
-int sb600_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
-{
-	return spi_write_chunked(flash, buf, start, len, 5);
-}
-
 static void reset_internal_fifo_pointer(void)
 {
 	mmio_writeb(mmio_readb(sb600_spibar + 2) | 0x10, sb600_spibar + 2);
diff --git a/spi.c b/spi.c
index 7e9ff1a..5857fda 100644
--- a/spi.c
+++ b/spi.c
@@ -34,6 +34,8 @@
 
 const struct spi_programmer spi_programmer[] = {
 	{ /* SPI_CONTROLLER_NONE */
+		.max_data_read = MAX_DATA_UNSPECIFIED,
+		.max_data_write = MAX_DATA_UNSPECIFIED,
 		.command = NULL,
 		.multicommand = NULL,
 		.read = NULL,
@@ -43,27 +45,35 @@
 #if CONFIG_INTERNAL == 1
 #if defined(__i386__) || defined(__x86_64__)
 	{ /* SPI_CONTROLLER_ICH7 */
+		.max_data_read = 64,
+		.max_data_write = 64,
 		.command = ich_spi_send_command,
 		.multicommand = ich_spi_send_multicommand,
-		.read = ich_spi_read,
-		.write_256 = ich_spi_write_256,
+		.read = default_spi_read,
+		.write_256 = default_spi_write_256,
 	},
 
 	{ /* SPI_CONTROLLER_ICH9 */
+		.max_data_read = 64,
+		.max_data_write = 64,
 		.command = ich_spi_send_command,
 		.multicommand = ich_spi_send_multicommand,
-		.read = ich_spi_read,
-		.write_256 = ich_spi_write_256,
+		.read = default_spi_read,
+		.write_256 = default_spi_write_256,
 	},
 
 	{ /* SPI_CONTROLLER_IT85XX */
+		.max_data_read = 64,
+		.max_data_write = 64,
 		.command = it85xx_spi_send_command,
 		.multicommand = default_spi_send_multicommand,
-		.read = it85_spi_read,
-		.write_256 = it85_spi_write_256,
+		.read = default_spi_read,
+		.write_256 = default_spi_write_256,
 	},
 
 	{ /* SPI_CONTROLLER_IT87XX */
+		.max_data_read = MAX_DATA_UNSPECIFIED,
+		.max_data_write = MAX_DATA_UNSPECIFIED,
 		.command = it8716f_spi_send_command,
 		.multicommand = default_spi_send_multicommand,
 		.read = it8716f_spi_chip_read,
@@ -71,20 +81,26 @@
 	},
 
 	{ /* SPI_CONTROLLER_SB600 */
+		.max_data_read = 8,
+		.max_data_write = 5,
 		.command = sb600_spi_send_command,
 		.multicommand = default_spi_send_multicommand,
-		.read = sb600_spi_read,
-		.write_256 = sb600_spi_write_256,
+		.read = default_spi_read,
+		.write_256 = default_spi_write_256,
 	},
 
 	{ /* SPI_CONTROLLER_VIA */
+		.max_data_read = 16,
+		.max_data_write = 16,
 		.command = ich_spi_send_command,
 		.multicommand = ich_spi_send_multicommand,
-		.read = ich_spi_read,
-		.write_256 = ich_spi_write_256,
+		.read = default_spi_read,
+		.write_256 = default_spi_write_256,
 	},
 
 	{ /* SPI_CONTROLLER_WBSIO */
+		.max_data_read = MAX_DATA_UNSPECIFIED,
+		.max_data_write = MAX_DATA_UNSPECIFIED,
 		.command = wbsio_spi_send_command,
 		.multicommand = default_spi_send_multicommand,
 		.read = wbsio_spi_read,
@@ -92,43 +108,53 @@
 	},
 
 	{ /* SPI_CONTROLLER_MCP6X_BITBANG */
+		.max_data_read = MAX_DATA_READ_UNLIMITED,
+		.max_data_write = MAX_DATA_WRITE_UNLIMITED,
 		.command = bitbang_spi_send_command,
 		.multicommand = default_spi_send_multicommand,
-		.read = bitbang_spi_read,
-		.write_256 = bitbang_spi_write_256,
+		.read = default_spi_read,
+		.write_256 = default_spi_write_256,
 	},
 #endif
 #endif
 
 #if CONFIG_FT2232_SPI == 1
 	{ /* SPI_CONTROLLER_FT2232 */
+		.max_data_read = 64 * 1024,
+		.max_data_write = 256,
 		.command = ft2232_spi_send_command,
 		.multicommand = default_spi_send_multicommand,
-		.read = ft2232_spi_read,
-		.write_256 = ft2232_spi_write_256,
+		.read = default_spi_read,
+		.write_256 = default_spi_write_256,
 	},
 #endif
 
 #if CONFIG_DUMMY == 1
 	{ /* SPI_CONTROLLER_DUMMY */
+		.max_data_read = MAX_DATA_READ_UNLIMITED,
+		.max_data_write = MAX_DATA_UNSPECIFIED,
 		.command = dummy_spi_send_command,
 		.multicommand = default_spi_send_multicommand,
-		.read = dummy_spi_read,
+		.read = default_spi_read,
 		.write_256 = dummy_spi_write_256,
 	},
 #endif
 
 #if CONFIG_BUSPIRATE_SPI == 1
 	{ /* SPI_CONTROLLER_BUSPIRATE */
+		.max_data_read = 12,
+		.max_data_write = 12,
 		.command = buspirate_spi_send_command,
 		.multicommand = default_spi_send_multicommand,
-		.read = buspirate_spi_read,
-		.write_256 = buspirate_spi_write_256,
+		.read = default_spi_read,
+		.write_256 = default_spi_write_256,
 	},
 #endif
 
 #if CONFIG_DEDIPROG == 1
 	{ /* SPI_CONTROLLER_DEDIPROG */
+		.max_data_read = MAX_DATA_UNSPECIFIED,
+		.max_data_write = MAX_DATA_UNSPECIFIED,
 		.command = dediprog_spi_send_command,
 		.multicommand = default_spi_send_multicommand,
 		.read = dediprog_spi_read,
@@ -138,28 +164,34 @@
 
 #if CONFIG_RAYER_SPI == 1
 	{ /* SPI_CONTROLLER_RAYER */
+		.max_data_read = MAX_DATA_READ_UNLIMITED,
+		.max_data_write = MAX_DATA_WRITE_UNLIMITED,
 		.command = bitbang_spi_send_command,
 		.multicommand = default_spi_send_multicommand,
-		.read = bitbang_spi_read,
-		.write_256 = bitbang_spi_write_256,
+		.read = default_spi_read,
+		.write_256 = default_spi_write_256,
 	},
 #endif
 
 #if CONFIG_NICINTEL_SPI == 1
 	{ /* SPI_CONTROLLER_NICINTEL */
+		.max_data_read = MAX_DATA_READ_UNLIMITED,
+		.max_data_write = MAX_DATA_WRITE_UNLIMITED,
 		.command = bitbang_spi_send_command,
 		.multicommand = default_spi_send_multicommand,
-		.read = bitbang_spi_read,
-		.write_256 = bitbang_spi_write_256,
+		.read = default_spi_read,
+		.write_256 = default_spi_write_256,
 	},
 #endif
 
 #if CONFIG_OGP_SPI == 1
 	{ /* SPI_CONTROLLER_OGP */
+		.max_data_read = MAX_DATA_READ_UNLIMITED,
+		.max_data_write = MAX_DATA_WRITE_UNLIMITED,
 		.command = bitbang_spi_send_command,
 		.multicommand = default_spi_send_multicommand,
-		.read = bitbang_spi_read,
-		.write_256 = bitbang_spi_write_256,
+		.read = default_spi_read,
+		.write_256 = default_spi_write_256,
 	},
 #endif
 
@@ -223,6 +255,30 @@
 	return result;
 }
 
+int default_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
+{
+	int max_data = spi_programmer[spi_controller].max_data_read;
+	if (max_data == MAX_DATA_UNSPECIFIED) {
+		msg_perr("%s called, but SPI read chunk size not defined "
+			 "on this hardware. Please report a bug at "
+			 "flashrom@flashrom.org\n", __func__);
+		return 1;
+	}
+	return spi_read_chunked(flash, buf, start, len, max_data);
+}
+
+int default_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
+{
+	int max_data = spi_programmer[spi_controller].max_data_write;
+	if (max_data == MAX_DATA_UNSPECIFIED) {
+		msg_perr("%s called, but SPI write chunk size not defined "
+			 "on this hardware. Please report a bug at "
+			 "flashrom@flashrom.org\n", __func__);
+		return 1;
+	}
+	return spi_write_chunked(flash, buf, start, len, max_data);
+}
+
 int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len)
 {
 	int addrbase = 0;