public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Ladislav Michl <Ladislav.Michl@seznam.cz>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH] CFI: fix eraseregions numblocks
Date: Thu, 28 Jan 2010 12:27:14 +0100	[thread overview]
Message-ID: <20100128112714.GA31435@localhost.localdomain> (raw)
In-Reply-To: <20100127231320.GA18587@localhost.localdomain>

From: Ladislav Michl <ladis@linux-mips.org>

eraseregions numblocks was sometimes one less than actual, possibly producing
erase regions with zero blocks. As MTD code touches eraseregions only if
numeraseregions is greater that zero, allocate eraseregions only for non
uniform erase size flash.

Signed-off-by: Ladislav Michl <ladis@linux-mips.org>
---
 cfi_mtd.c |   35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/drivers/mtd/cfi_mtd.c b/drivers/mtd/cfi_mtd.c
index 34748dd..6a0cab3 100644
--- a/drivers/mtd/cfi_mtd.c
+++ b/drivers/mtd/cfi_mtd.c
@@ -161,8 +161,8 @@ static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
 	int sect;
 	int regions = 0;
 	int numblocks = 0;
-	ulong offset = 0;
-	ulong base_addr = fi->start[0];
+	ulong offset;
+	ulong base_addr;
 
 	/*
 	 * First detect the number of eraseregions so that we can allocate
@@ -174,29 +174,35 @@ static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
 		sect_size_old = flash_sector_size(fi, sect);
 	}
 
+	switch (regions) {
+	case 0:
+		return 1;
+	case 1:	/* flash has uniform erase size */
+		mtd->numeraseregions = 0;
+		mtd->erasesize = sect_size_old;
+		return 0;
+	}
+
+	mtd->numeraseregions = regions;
 	mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info) * regions);
 
 	/*
 	 * Now detect the largest sector and fill the eraseregions
 	 */
-	sect_size_old = 0;
 	regions = 0;
+	base_addr = offset = fi->start[0];
+	sect_size_old = flash_sector_size(fi, 0);
 	for (sect = 0; sect < fi->sector_count; sect++) {
-		if ((sect_size_old != flash_sector_size(fi, sect)) &&
-		    (sect_size_old != 0)) {
+		if (sect_size_old != flash_sector_size(fi, sect)) {
 			mtd->eraseregions[regions].offset = offset - base_addr;
 			mtd->eraseregions[regions].erasesize = sect_size_old;
 			mtd->eraseregions[regions].numblocks = numblocks;
-
 			/* Now start counting the next eraseregions */
 			numblocks = 0;
 			regions++;
-		} else {
-			numblocks++;
-		}
-
-		if (sect_size_old != flash_sector_size(fi, sect))
 			offset = fi->start[sect];
+		}
+		numblocks++;
 
 		/*
 		 * Select the largest sector size as erasesize (e.g. for UBI)
@@ -212,12 +218,7 @@ static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
 	 */
 	mtd->eraseregions[regions].offset = offset - base_addr;
 	mtd->eraseregions[regions].erasesize = sect_size_old;
-	mtd->eraseregions[regions].numblocks = numblocks + 1;
-
-	if (regions)
-		mtd->numeraseregions = regions + 1;
-	else
-		mtd->numeraseregions = 0;
+	mtd->eraseregions[regions].numblocks = numblocks;
 
 	mtd->erasesize = sect_size;
 

  reply	other threads:[~2010-01-28 11:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-27 23:13 [U-Boot] CFI numblocks incorrect? Ladislav Michl
2010-01-28 11:27 ` Ladislav Michl [this message]
2010-02-02  8:12   ` [U-Boot] [PATCH] CFI: fix eraseregions numblocks Stefan Roese

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=20100128112714.GA31435@localhost.localdomain \
    --to=ladislav.michl@seznam.cz \
    --cc=u-boot@lists.denx.de \
    /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