Patchwork Convert SPI byte program to multicommand

login
register
about
Submitter Carl-Daniel Hailfinger
Date 2009-07-12 01:09:52
Message ID <4A5937E0.7080900@gmx.net>
Download mbox | patch
Permalink /patch/28/
State Accepted
Commit r651
Headers show

Comments

Carl-Daniel Hailfinger - 2009-07-12 01:09:52
New version, fixes nbyte-program length.

Convert SPI byte program to use the multicommand infrastructure.

Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
Jakob Bornecrantz - 2009-07-12 02:04:06
On Sun, Jul 12, 2009 at 3:09 AM, Carl-Daniel
Hailfinger<c-d.hailfinger.devel.2006@gmx.net> wrote:
> New version, fixes nbyte-program length.
>
> Convert SPI byte program to use the multicommand infrastructure.
>
> Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>

Tested it on Epia-m700 worked okay.

Just one comment couldn't write_byte just call into write_bytes(addr,
{byte}, 1) otherwise it is fine.

Acked-by: Jakob Bornecrantz <wallbraker@gmail.com>
Tested-by: Jakob Bornecrantz <wallbraker@gmail.com>

Cheers Jakob.
Carl-Daniel Hailfinger - 2009-07-12 12:08:51
On 12.07.2009 04:04, Jakob Bornecrantz wrote:
> On Sun, Jul 12, 2009 at 3:09 AM, Carl-Daniel
> Hailfinger<c-d.hailfinger.devel.2006@gmx.net> wrote:
>   
>> Convert SPI byte program to use the multicommand infrastructure.
>>
>> Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
>>     
>
> Tested it on Epia-m700 worked okay.
>
> Just one comment couldn't write_byte just call into write_bytes(addr,
> {byte}, 1) otherwise it is fine.
>   

The write_byte stuff needs to be reworked, true. I'm trying to come up
with a reasonably efficient way to do this.

> Acked-by: Jakob Bornecrantz <wallbraker@gmail.com>
> Tested-by: Jakob Bornecrantz <wallbraker@gmail.com>
>   

Thanks, r651.

Regards,
Carl-Daniel

Patch

Index: flashrom-spi_multicommand_byteprogram/flash.h
===================================================================
--- flashrom-spi_multicommand_byteprogram/flash.h	(Revision 650)
+++ flashrom-spi_multicommand_byteprogram/flash.h	(Arbeitskopie)
@@ -446,9 +446,9 @@ 
 int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len);
 uint8_t spi_read_status_register(void);
 int spi_disable_blockprotect(void);
-void spi_byte_program(int address, uint8_t byte);
-int spi_nbyte_program(int address, uint8_t *bytes, int len);
-int spi_nbyte_read(int address, uint8_t *bytes, int len);
+int spi_byte_program(int addr, uint8_t byte);
+int spi_nbyte_program(int addr, uint8_t *bytes, int len);
+int spi_nbyte_read(int addr, uint8_t *bytes, int len);
 int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize);
 int spi_aai_write(struct flashchip *flash, uint8_t *buf);
 uint32_t spi_get_valid_read_addr(void);
Index: flashrom-spi_multicommand_byteprogram/it87spi.c
===================================================================
--- flashrom-spi_multicommand_byteprogram/it87spi.c	(Revision 650)
+++ flashrom-spi_multicommand_byteprogram/it87spi.c	(Arbeitskopie)
@@ -232,6 +232,7 @@ 
 	result = spi_write_enable();
 	if (result)
 		return result;
+	/* FIXME: The command below seems to be redundant or wrong. */
 	OUTB(0x06, it8716f_flashport + 1);
 	OUTB(((2 + (fast_spi ? 1 : 0)) << 4), it8716f_flashport);
 	for (i = 0; i < 256; i++) {
@@ -262,10 +263,7 @@ 
 
 	spi_disable_blockprotect();
 	for (i = 0; i < total_size; i++) {
-		result = spi_write_enable();
-		if (result)
-			return result;
-		spi_byte_program(i, buf[i]);
+		result = spi_byte_program(i, buf[i]);
 		while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
 			programmer_delay(10);
 	}
Index: flashrom-spi_multicommand_byteprogram/spi.c
===================================================================
--- flashrom-spi_multicommand_byteprogram/spi.c	(Revision 650)
+++ flashrom-spi_multicommand_byteprogram/spi.c	(Arbeitskopie)
@@ -154,21 +154,7 @@ 
 	result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
 
 	if (result)
-		printf_debug("%s failed", __func__);
-	if (result == SPI_INVALID_OPCODE) {
-		switch (spi_controller) {
-		case SPI_CONTROLLER_ICH7:
-		case SPI_CONTROLLER_ICH9:
-		case SPI_CONTROLLER_VIA:
-			printf_debug(" due to SPI master limitation, ignoring"
-				     " and hoping it will be run as PREOP\n");
-			return 0;
-		default:
-			break;
-		}
-	}
-	if (result)
-		printf_debug("\n");
+		printf_debug("%s failed\n", __func__);
 
 	return result;
 }
@@ -736,39 +722,80 @@ 
 	return spi_send_command(sizeof(cmd), 0, cmd, NULL);
 }
 
-void spi_byte_program(int address, uint8_t byte)
+int spi_byte_program(int addr, uint8_t byte)
 {
-	const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {
-		JEDEC_BYTE_PROGRAM,
-		(address >> 16) & 0xff,
-		(address >> 8) & 0xff,
-		(address >> 0) & 0xff,
-		byte
-	};
+	int result;
+	struct spi_command spicommands[] = {
+	{
+		.writecnt	= JEDEC_WREN_OUTSIZE,
+		.writearr	= (const unsigned char[]){ JEDEC_WREN },
+		.readcnt	= 0,
+		.readarr	= NULL,
+	}, {
+		.writecnt	= JEDEC_BYTE_PROGRAM_OUTSIZE,
+		.writearr	= (const unsigned char[]){ JEDEC_BYTE_PROGRAM, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff), byte },
+		.readcnt	= 0,
+		.readarr	= NULL,
+	}, {
+		.writecnt	= 0,
+		.writearr	= NULL,
+		.readcnt	= 0,
+		.readarr	= NULL,
+	}};
 
-	/* Send Byte-Program */
-	spi_send_command(sizeof(cmd), 0, cmd, NULL);
+	result = spi_send_multicommand(spicommands);
+	if (result) {
+		printf_debug("%s failed during command execution\n", __func__);
+		return result;
+	}
+	return result;
 }
 
 int spi_nbyte_program(int address, uint8_t *bytes, int len)
 {
+	int result;
+	/* FIXME: Switch to malloc based on len unless that kills speed. */
 	unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
 		JEDEC_BYTE_PROGRAM,
 		(address >> 16) & 0xff,
 		(address >> 8) & 0xff,
 		(address >> 0) & 0xff,
 	};
+	struct spi_command spicommands[] = {
+	{
+		.writecnt	= JEDEC_WREN_OUTSIZE,
+		.writearr	= (const unsigned char[]){ JEDEC_WREN },
+		.readcnt	= 0,
+		.readarr	= NULL,
+	}, {
+		.writecnt	= JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
+		.writearr	= cmd,
+		.readcnt	= 0,
+		.readarr	= NULL,
+	}, {
+		.writecnt	= 0,
+		.writearr	= NULL,
+		.readcnt	= 0,
+		.readarr	= NULL,
+	}};
 
+	if (!len) {
+		printf_debug ("%s called for zero-length write\n", __func__);
+		return 1;
+	}
 	if (len > 256) {
-		printf_debug ("%s called for too long a write\n",
-		     __FUNCTION__);
+		printf_debug ("%s called for too long a write\n", __func__);
 		return 1;
 	}
 
 	memcpy(&cmd[4], bytes, len);
 
-	/* Send Byte-Program */
-	return spi_send_command(4 + len, 0, cmd, NULL);
+	result = spi_send_multicommand(spicommands);
+	if (result) {
+		printf_debug("%s failed during command execution\n", __func__);
+		return result;
+	}
+	return result;
 }
 
 int spi_disable_blockprotect(void)
@@ -883,7 +910,6 @@ 
 
 	spi_disable_blockprotect();
 	for (i = 0; i < total_size; i++) {
-		spi_write_enable();
 		spi_byte_program(i, buf[i]);
 		while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
 			programmer_delay(10);
Index: flashrom-spi_multicommand_byteprogram/ft2232_spi.c
===================================================================
--- flashrom-spi_multicommand_byteprogram/ft2232_spi.c	(Revision 650)
+++ flashrom-spi_multicommand_byteprogram/ft2232_spi.c	(Arbeitskopie)
@@ -276,7 +276,6 @@ 
 		else
 			l = total_size - i;
 
-		spi_write_enable();
 		if ((r = spi_nbyte_program(i, &buf[i], l))) {
 			fprintf(stderr, "%s: write fail %d\n", __FUNCTION__, r);
 			// spi_write_disable();  chip does this for us
Index: flashrom-spi_multicommand_byteprogram/wbsio_spi.c
===================================================================
--- flashrom-spi_multicommand_byteprogram/wbsio_spi.c	(Revision 650)
+++ flashrom-spi_multicommand_byteprogram/wbsio_spi.c	(Arbeitskopie)
@@ -200,11 +200,8 @@ 
 		fprintf(stderr, "ERASE FAILED!\n");
 		return -1;
 	}
-	result = spi_write_enable();
-	if (result)
-		return result;
 	for (pos = 0; pos < size; pos++) {
-		spi_byte_program(pos, buf[pos]);
+		result = spi_byte_program(pos, buf[pos]);
 		while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
 			programmer_delay(10);
 	}
Index: flashrom-spi_multicommand_byteprogram/sb600spi.c
===================================================================
--- flashrom-spi_multicommand_byteprogram/sb600spi.c	(Revision 650)
+++ flashrom-spi_multicommand_byteprogram/sb600spi.c	(Arbeitskopie)
@@ -73,10 +73,7 @@ 
 	printf("Programming flash");
 	for (i = 0; i < total_size; i++, buf++) {
 		spi_disable_blockprotect();
-		result = spi_write_enable();
-		if (result)
-			return result;
-		spi_byte_program(i, *buf);
+		result = spi_byte_program(i, *buf);
 		/* wait program complete. */
 		if (i % 0x8000 == 0)
 			printf(".");