diff mbox

[U-Boot,13/17] eeprom: Pull out transfer length computation

Message ID 1447185213-5799-13-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
Pull out the code which computes the length of the transfer
into separate code and clean it up a little. 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 | 66 ++++++++++++++++++++---------------------------------
 1 file changed, 25 insertions(+), 41 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:
> Pull out the code which computes the length of the transfer
> into separate code and clean it up a little. 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 | 66 ++++++++++++++++++++---------------------------------
>   1 file changed, 25 insertions(+), 41 deletions(-)


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

bye,
Heiko
>
> diff --git a/common/cmd_eeprom.c b/common/cmd_eeprom.c
> index 162a05c..b39ca5d 100644
> --- a/common/cmd_eeprom.c
> +++ b/common/cmd_eeprom.c
> @@ -95,6 +95,29 @@ static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
>   	return alen;
>   }
>
> +static int eeprom_len(unsigned offset, unsigned end)
> +{
> +	unsigned len = end - offset;
> +
> +	/*
> +	 * For a FRAM device there is no limit on the number of the
> +	 * bytes that can be ccessed with the single read or write
> +	 * operation.
> +	 */
> +#if !defined(CONFIG_SYS_I2C_FRAM)
> +	unsigned blk_off = offset & 0xff;
> +	unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
> +
> +	if (maxlen > I2C_RXTX_LEN)
> +		maxlen = I2C_RXTX_LEN;
> +
> +	if (len > maxlen)
> +		len = maxlen;
> +#endif
> +
> +	return len;
> +}
> +
>   static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
>   			   uchar *buffer, unsigned len, bool read)
>   {
> @@ -126,7 +149,6 @@ static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
>   int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
>   {
>   	unsigned end = offset + cnt;
> -	unsigned blk_off;
>   	int rcode = 0;
>   	uchar addr[3];
>
> @@ -137,27 +159,10 @@ int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt
>   	 */
>   	while (offset < end) {
>   		unsigned alen, len;
> -#if !defined(CONFIG_SYS_I2C_FRAM)
> -		unsigned maxlen;
> -#endif
>
> -		blk_off = offset & 0xFF;	/* block offset */
>   		alen = eeprom_addr(dev_addr, offset, addr);
>
> -		len = end - offset;
> -
> -		/*
> -		 * For a FRAM device there is no limit on the number of the
> -		 * bytes that can be ccessed with the single read or write
> -		 * operation.
> -		 */
> -#if !defined(CONFIG_SYS_I2C_FRAM)
> -		maxlen = 0x100 - blk_off;
> -		if (maxlen > I2C_RXTX_LEN)
> -			maxlen = I2C_RXTX_LEN;
> -		if (len > maxlen)
> -			len = maxlen;
> -#endif
> +		len = eeprom_len(offset, end);
>
>   		rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 1);
>
> @@ -171,7 +176,6 @@ int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt
>   int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
>   {
>   	unsigned end = offset + cnt;
> -	unsigned blk_off;
>   	int rcode = 0;
>   	uchar addr[3];
>
> @@ -185,30 +189,10 @@ int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cn
>
>   	while (offset < end) {
>   		unsigned alen, len;
> -#if !defined(CONFIG_SYS_I2C_FRAM)
> -		unsigned maxlen;
> -#endif
>
> -		blk_off = offset & 0xFF;	/* block offset */
>   		alen = eeprom_addr(dev_addr, offset, addr);
>
> -		len = end - offset;
> -
> -		/*
> -		 * For a FRAM device there is no limit on the number of the
> -		 * bytes that can be accessed with the single read or write
> -		 * operation.
> -		 */
> -#if !defined(CONFIG_SYS_I2C_FRAM)
> -
> -		maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
> -
> -		if (maxlen > I2C_RXTX_LEN)
> -			maxlen = I2C_RXTX_LEN;
> -
> -		if (len > maxlen)
> -			len = maxlen;
> -#endif
> +		len = eeprom_len(offset, end);
>
>   		rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 0);
>
>
Tom Rini Nov. 22, 2015, 3:54 p.m. UTC | #2
On Tue, Nov 10, 2015 at 08:53:29PM +0100, Marek Vasut wrote:

> Pull out the code which computes the length of the transfer
> into separate code and clean it up a little. 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 162a05c..b39ca5d 100644
--- a/common/cmd_eeprom.c
+++ b/common/cmd_eeprom.c
@@ -95,6 +95,29 @@  static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
 	return alen;
 }
 
+static int eeprom_len(unsigned offset, unsigned end)
+{
+	unsigned len = end - offset;
+
+	/*
+	 * For a FRAM device there is no limit on the number of the
+	 * bytes that can be ccessed with the single read or write
+	 * operation.
+	 */
+#if !defined(CONFIG_SYS_I2C_FRAM)
+	unsigned blk_off = offset & 0xff;
+	unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
+
+	if (maxlen > I2C_RXTX_LEN)
+		maxlen = I2C_RXTX_LEN;
+
+	if (len > maxlen)
+		len = maxlen;
+#endif
+
+	return len;
+}
+
 static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
 			   uchar *buffer, unsigned len, bool read)
 {
@@ -126,7 +149,6 @@  static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
 int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
 {
 	unsigned end = offset + cnt;
-	unsigned blk_off;
 	int rcode = 0;
 	uchar addr[3];
 
@@ -137,27 +159,10 @@  int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt
 	 */
 	while (offset < end) {
 		unsigned alen, len;
-#if !defined(CONFIG_SYS_I2C_FRAM)
-		unsigned maxlen;
-#endif
 
-		blk_off = offset & 0xFF;	/* block offset */
 		alen = eeprom_addr(dev_addr, offset, addr);
 
-		len = end - offset;
-
-		/*
-		 * For a FRAM device there is no limit on the number of the
-		 * bytes that can be ccessed with the single read or write
-		 * operation.
-		 */
-#if !defined(CONFIG_SYS_I2C_FRAM)
-		maxlen = 0x100 - blk_off;
-		if (maxlen > I2C_RXTX_LEN)
-			maxlen = I2C_RXTX_LEN;
-		if (len > maxlen)
-			len = maxlen;
-#endif
+		len = eeprom_len(offset, end);
 
 		rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 1);
 
@@ -171,7 +176,6 @@  int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt
 int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
 {
 	unsigned end = offset + cnt;
-	unsigned blk_off;
 	int rcode = 0;
 	uchar addr[3];
 
@@ -185,30 +189,10 @@  int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cn
 
 	while (offset < end) {
 		unsigned alen, len;
-#if !defined(CONFIG_SYS_I2C_FRAM)
-		unsigned maxlen;
-#endif
 
-		blk_off = offset & 0xFF;	/* block offset */
 		alen = eeprom_addr(dev_addr, offset, addr);
 
-		len = end - offset;
-
-		/*
-		 * For a FRAM device there is no limit on the number of the
-		 * bytes that can be accessed with the single read or write
-		 * operation.
-		 */
-#if !defined(CONFIG_SYS_I2C_FRAM)
-
-		maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
-
-		if (maxlen > I2C_RXTX_LEN)
-			maxlen = I2C_RXTX_LEN;
-
-		if (len > maxlen)
-			len = maxlen;
-#endif
+		len = eeprom_len(offset, end);
 
 		rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 0);