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] mtd: Align erase_op to eraseblock boundaries
Date: Thu,  6 Jun 2013 16:20:20 +0200	[thread overview]
Message-ID: <1370528420-4388-1-git-send-email-s.hauer@pengutronix.de> (raw)

Our erase command used to align to eraseblocks when necessary.
This worked well until recently when the m25p80, mtd_dataflash
and cfi flash were added / converted to mtd. This patch aligns
the input to the erase fileoperation to eraseblock boundaries.
Also tested with non uniform flashes with multiple eraseregions.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/mtd/core.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/drivers/mtd/core.c b/drivers/mtd/core.c
index f358098..fe9ec1d 100644
--- a/drivers/mtd/core.c
+++ b/drivers/mtd/core.c
@@ -98,12 +98,60 @@ static ssize_t mtd_op_write(struct cdev* cdev, const void *buf, size_t _count,
 	return ret ? ret : _count;
 }
 
+static struct mtd_erase_region_info *mtd_find_erase_region(struct mtd_info *mtd, loff_t offset)
+{
+	int i;
+
+	for (i = 0; i < mtd->numeraseregions; i++) {
+		struct mtd_erase_region_info *e = &mtd->eraseregions[i];
+		if (offset > e->offset + e->erasesize * e->numblocks)
+			continue;
+		return e;
+	}
+
+	return NULL;
+}
+
+static int mtd_erase_align(struct mtd_info *mtd, size_t *count, loff_t *offset)
+{
+	struct mtd_erase_region_info *e;
+	loff_t ofs;
+
+	if (mtd->numeraseregions == 0) {
+		ofs = *offset & ~(mtd->erasesize - 1);
+		*count += (*offset - ofs);
+		*count = ALIGN(*count, mtd->erasesize);
+		*offset = ofs;
+		return 0;
+	}
+
+	e = mtd_find_erase_region(mtd, *offset);
+	if (!e)
+		return -EINVAL;
+
+	ofs = *offset & ~(e->erasesize - 1);
+	*count += (*offset - ofs);
+
+	e = mtd_find_erase_region(mtd, *offset + *count);
+	if (!e)
+		return -EINVAL;
+
+	*count = ALIGN(*count, e->erasesize);
+	*offset = ofs;
+
+	return 0;
+}
+
 static int mtd_op_erase(struct cdev *cdev, size_t count, loff_t offset)
 {
 	struct mtd_info *mtd = cdev->priv;
 	struct erase_info erase;
 	int ret;
 
+	ret = mtd_erase_align(mtd, &count, &offset);
+	if (ret)
+		return ret;
+
 	memset(&erase, 0, sizeof(erase));
 	erase.mtd = mtd;
 	erase.addr = offset;
-- 
1.8.2.rc2


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

                 reply	other threads:[~2013-06-06 14:20 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1370528420-4388-1-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