mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 2/2] cfi flash: Fix alignment problem
Date: Thu,  5 Apr 2012 10:59:15 +0200	[thread overview]
Message-ID: <1333616355-632-3-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1333616355-632-1-git-send-email-s.hauer@pengutronix.de>

The intel cfi buffer write has a problem with writing when
the alignment of the buffer in memory is smaller than the
flash bus width.

This patch fixes a alignment problem which may show during this
scenario:
- 32 or 64 attached NOR flash
- flashing an image directly from network to the nor flash

The involved network driver is "smc9111.c".

The data that comes from the network stack and should be written into
the flash isn't 32 bit aligned (at least with this network driver).
This is probably due to the 48 bit wide ethernet addresses.

However the "cfi_flash.c" driver doesn't handle this situation, and
accesses the not-aligned address with a 32 bit pointer.

This patch fixes the problem by reducing the access width if an
aligment problem between source and destination is found.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/nor/cfi_flash_intel.c |   18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/drivers/nor/cfi_flash_intel.c b/drivers/nor/cfi_flash_intel.c
index f0cbf72..32e581a 100644
--- a/drivers/nor/cfi_flash_intel.c
+++ b/drivers/nor/cfi_flash_intel.c
@@ -57,6 +57,9 @@ static int intel_flash_write_cfibuffer (struct flash_info *info, ulong dest, con
 	int retcode;
 	void *src = (void*)cp;
 	void *dst = (void *)dest;
+	/* reduce width due to possible alignment problems */
+	const unsigned long ptr = (unsigned long)dest | (unsigned long)cp | info->portwidth;
+	const int width = ptr & -ptr;
 
 	sector = find_sector (info, dest);
 	flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
@@ -70,20 +73,25 @@ static int intel_flash_write_cfibuffer (struct flash_info *info, ulong dest, con
 	/* reduce the number of loops by the width of the port	*/
 	cnt = len / width;
 
-	flash_write_cmd(info, sector, 0, (u32)cnt - 1);
+	flash_write_cmd(info, sector, 0, cnt - 1);
 	while (cnt-- > 0) {
-		if (bankwidth_is_1(info)) {
+		switch (width) {
+		case 1:
 			flash_write8(flash_read8(src), dst);
 			src += 1, dst += 1;
-		} else if (bankwidth_is_2(info)) {
+			break;
+		case 2:
 			flash_write16(flash_read16(src), dst);
 			src += 2, dst += 2;
-		} else if (bankwidth_is_4(info)) {
+			break;
+		case 4:
 			flash_write32(flash_read32(src), dst);
 			src += 4, dst += 4;
-		} else if (bankwidth_is_8(info)) {
+			break;
+		case 8:
 			flash_write64(flash_read64(src), dst);
 			src += 8, dst += 8;
+			break;
 		}
 	}
 
-- 
1.7.9.5


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2012-04-05  8:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-05  8:59 cfi driver fixes Sascha Hauer
2012-04-05  8:59 ` [PATCH 1/2] cfi flash: Fix loop count calculation Sascha Hauer
2012-04-10  8:38   ` Eric Bénard
2012-04-05  8:59 ` Sascha Hauer [this message]
  -- strict thread matches above, loose matches on Subject: below --
2010-02-01 10:31 [PATCH 0/2] smc91111 mac address and cfi flash fixes Marc Kleine-Budde
2010-02-01 10:31 ` [PATCH 1/2] smc91111: fix odering of mac address read from EEPROM Marc Kleine-Budde
2010-02-01 10:31   ` [PATCH 2/2] cfi_flash: fix alignment problem Marc Kleine-Budde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1333616355-632-3-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox