diff mbox

[U-Boot,14/17] eeprom: Pull out the RW loop

Message ID 1447185213-5799-14-git-send-email-marex@denx.de
State Accepted
Delegated to: Tom Rini
Headers show

Commit Message

Marek Vasut Nov. 10, 2015, 7:53 p.m. UTC
Unify the code for doing read/write into single function, since the
code for both the read and write is almost identical. This again
trims down the code duplication.

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Simon Glass <sjg@chromium.org>
Cc: Tom Rini <trini@konsulko.com>
Cc: Heiko Schocher <hs@denx.de>
---
 common/cmd_eeprom.c | 51 +++++++++++++++++++++------------------------------
 1 file changed, 21 insertions(+), 30 deletions(-)

Comments

Heiko Schocher Nov. 16, 2015, 11:31 a.m. UTC | #1
Hello Marek,

Am 10.11.2015 um 20:53 schrieb Marek Vasut:
> Unify the code for doing read/write into single function, since the
> code for both the read and write is almost identical. This again
> trims down the code duplication.
>
> Signed-off-by: Marek Vasut <marex@denx.de>
> Cc: Simon Glass <sjg@chromium.org>
> Cc: Tom Rini <trini@konsulko.com>
> Cc: Heiko Schocher <hs@denx.de>
> ---
>   common/cmd_eeprom.c | 51 +++++++++++++++++++++------------------------------
>   1 file changed, 21 insertions(+), 30 deletions(-)

Reviewed-by: Heiko Schocher <hs@denx.de>

bye,
Heiko
>
> diff --git a/common/cmd_eeprom.c b/common/cmd_eeprom.c
> index b39ca5d..d0942ef 100644
> --- a/common/cmd_eeprom.c
> +++ b/common/cmd_eeprom.c
> @@ -146,38 +146,44 @@ static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
>   	return ret;
>   }
>
> -int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
> +static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
> +		     unsigned cnt, bool read)
>   {
>   	unsigned end = offset + cnt;
> +	unsigned alen, len;
>   	int rcode = 0;
>   	uchar addr[3];
>
> -	/*
> -	 * Read data until done or would cross a page boundary.
> -	 * We must write the address again when changing pages
> -	 * because the next page may be in a different device.
> -	 */
>   	while (offset < end) {
> -		unsigned alen, len;
> -
>   		alen = eeprom_addr(dev_addr, offset, addr);
>
>   		len = eeprom_len(offset, end);
>
> -		rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 1);
> +		rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
>
>   		buffer += len;
>   		offset += len;
> +
> +		if (!read)
> +			udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
>   	}
>
>   	return rcode;
>   }
>
> -int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
> +int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
>   {
> -	unsigned end = offset + cnt;
> -	int rcode = 0;
> -	uchar addr[3];
> +	/*
> +	 * Read data until done or would cross a page boundary.
> +	 * We must write the address again when changing pages
> +	 * because the next page may be in a different device.
> +	 */
> +	return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
> +}
> +
> +int eeprom_write(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
> +{
> +	int ret;
>
>   	eeprom_write_enable(dev_addr, 1);
>
> @@ -186,25 +192,10 @@ int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cn
>   	 * We must write the address again when changing pages
>   	 * because the address counter only increments within a page.
>   	 */
> -
> -	while (offset < end) {
> -		unsigned alen, len;
> -
> -		alen = eeprom_addr(dev_addr, offset, addr);
> -
> -		len = eeprom_len(offset, end);
> -
> -		rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 0);
> -
> -		buffer += len;
> -		offset += len;
> -
> -		udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
> -	}
> +	ret = eeprom_rw(dev_addr, offset, buffer, cnt, 1);
>
>   	eeprom_write_enable(dev_addr, 0);
> -
> -	return rcode;
> +	return ret;
>   }
>
>   static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
>
Tom Rini Nov. 22, 2015, 3:54 p.m. UTC | #2
On Tue, Nov 10, 2015 at 08:53:30PM +0100, Marek Vasut wrote:

> Unify the code for doing read/write into single function, since the
> code for both the read and write is almost identical. This again
> trims down the code duplication.
> 
> Signed-off-by: Marek Vasut <marex@denx.de>
> Cc: Simon Glass <sjg@chromium.org>
> Cc: Tom Rini <trini@konsulko.com>
> Cc: Heiko Schocher <hs@denx.de>
> Reviewed-by: Heiko Schocher <hs@denx.de>

Applied to u-boot/master, thanks!
diff mbox

Patch

diff --git a/common/cmd_eeprom.c b/common/cmd_eeprom.c
index b39ca5d..d0942ef 100644
--- a/common/cmd_eeprom.c
+++ b/common/cmd_eeprom.c
@@ -146,38 +146,44 @@  static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
 	return ret;
 }
 
-int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
+static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
+		     unsigned cnt, bool read)
 {
 	unsigned end = offset + cnt;
+	unsigned alen, len;
 	int rcode = 0;
 	uchar addr[3];
 
-	/*
-	 * Read data until done or would cross a page boundary.
-	 * We must write the address again when changing pages
-	 * because the next page may be in a different device.
-	 */
 	while (offset < end) {
-		unsigned alen, len;
-
 		alen = eeprom_addr(dev_addr, offset, addr);
 
 		len = eeprom_len(offset, end);
 
-		rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 1);
+		rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
 
 		buffer += len;
 		offset += len;
+
+		if (!read)
+			udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
 	}
 
 	return rcode;
 }
 
-int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
+int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
 {
-	unsigned end = offset + cnt;
-	int rcode = 0;
-	uchar addr[3];
+	/*
+	 * Read data until done or would cross a page boundary.
+	 * We must write the address again when changing pages
+	 * because the next page may be in a different device.
+	 */
+	return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
+}
+
+int eeprom_write(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
+{
+	int ret;
 
 	eeprom_write_enable(dev_addr, 1);
 
@@ -186,25 +192,10 @@  int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cn
 	 * We must write the address again when changing pages
 	 * because the address counter only increments within a page.
 	 */
-
-	while (offset < end) {
-		unsigned alen, len;
-
-		alen = eeprom_addr(dev_addr, offset, addr);
-
-		len = eeprom_len(offset, end);
-
-		rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 0);
-
-		buffer += len;
-		offset += len;
-
-		udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
-	}
+	ret = eeprom_rw(dev_addr, offset, buffer, cnt, 1);
 
 	eeprom_write_enable(dev_addr, 0);
-
-	return rcode;
+	return ret;
 }
 
 static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])