From: Marek Vasut Date: Mon, 20 Oct 2014 03:48:21 +0200 Subject: [U-Boot] [PATCH 20/25] eeprom: Pull out transfer length computation In-Reply-To: <1413769706-8596-1-git-send-email-marex@denx.de> References: <1413769706-8596-1-git-send-email-marex@denx.de> Message-ID: <1413769706-8596-21-git-send-email-marex@denx.de> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de 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 Cc: Simon Glass Cc: Tom Rini Cc: Heiko Schocher --- common/cmd_eeprom.c | 66 ++++++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/common/cmd_eeprom.c b/common/cmd_eeprom.c index 6f55306..231f424 100644 --- a/common/cmd_eeprom.c +++ b/common/cmd_eeprom.c @@ -104,6 +104,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) { @@ -130,7 +153,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]; @@ -141,27 +163,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); @@ -175,7 +180,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]; @@ -189,30 +193,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); -- 2.1.1