* [PATCH] mtd: Align erase_op to eraseblock boundaries
@ 2013-06-06 14:20 Sascha Hauer
0 siblings, 0 replies; only message in thread
From: Sascha Hauer @ 2013-06-06 14:20 UTC (permalink / raw)
To: barebox
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2013-06-06 14:20 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-06-06 14:20 [PATCH] mtd: Align erase_op to eraseblock boundaries Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox