mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 2/7] mtd: Fix mtdraw for Nand > 4GiB
Date: Tue,  9 Feb 2016 10:55:43 +0100	[thread overview]
Message-ID: <1455011748-5538-3-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1455011748-5538-1-git-send-email-s.hauer@pengutronix.de>

The mtdraw code has some casts to avoid 64bit divisions. For Chips
>4GiB using 64bit types become necessary, so use them and to the
necessary 64bit math.

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

diff --git a/drivers/mtd/mtdraw.c b/drivers/mtd/mtdraw.c
index ae4bec2..837dcd1 100644
--- a/drivers/mtd/mtdraw.c
+++ b/drivers/mtd/mtdraw.c
@@ -83,6 +83,20 @@ static struct mtd_info *to_mtd(struct cdev *cdev)
 	return mtdraw->mtd;
 }
 
+static unsigned int mtdraw_offset_to_block(struct mtd_info *mtd, loff_t offset)
+{
+	u64 ofs64 = offset;
+
+	do_div(ofs64, mtd->writesize + mtd->oobsize);
+
+	return ofs64;
+}
+
+static loff_t mtdraw_raw_to_mtd_offset(struct mtd_info *mtd, loff_t offset)
+{
+	return (loff_t)mtdraw_offset_to_block(mtd, offset) * mtd->writesize;
+}
+
 static ssize_t mtdraw_read_unaligned(struct mtd_info *mtd, void *dst,
 				     size_t count, int skip, ulong offset)
 {
@@ -117,22 +131,21 @@ err:
 }
 
 static ssize_t mtdraw_read(struct cdev *cdev, void *buf, size_t count,
-			    loff_t _offset, ulong flags)
+			    loff_t offset, ulong flags)
 {
 	struct mtd_info *mtd = to_mtd(cdev);
 	ssize_t retlen = 0, ret = 1, toread;
-	ulong numpage;
+	ulong numblock;
 	int skip;
-	unsigned long offset = _offset;
 
-	numpage = offset / (mtd->writesize + mtd->oobsize);
-	skip = offset % (mtd->writesize + mtd->oobsize);
+	numblock = mtdraw_offset_to_block(mtd, offset);
+	skip = offset - numblock * (mtd->writesize + mtd->oobsize);
 
 	while (ret > 0 && count > 0) {
 		toread = min_t(int, count,
 				mtd->writesize + mtd->oobsize - skip);
 		ret = mtdraw_read_unaligned(mtd, buf, toread,
-					    skip, numpage++ * mtd->writesize);
+					    skip, numblock++ * mtd->writesize);
 		buf += ret;
 		skip = 0;
 		count -= ret;
@@ -171,20 +184,21 @@ static void mtdraw_fillbuf(struct mtdraw *mtdraw, const void *src, int nbbytes)
 }
 
 static ssize_t mtdraw_write(struct cdev *cdev, const void *buf, size_t count,
-			    loff_t _offset, ulong flags)
+			    loff_t offset, ulong flags)
 {
 	struct mtdraw *mtdraw = to_mtdraw(cdev);
 	struct mtd_info *mtd = to_mtd(cdev);
 	int bsz = mtd->writesize + mtd->oobsize;
-	ulong numpage;
+	ulong numblock;
 	size_t retlen = 0, tofill;
-	unsigned long offset = _offset;
 	int ret = 0;
 
+	numblock = mtdraw_offset_to_block(mtd, offset);
+
 	if (mtdraw->write_fill &&
 	    mtdraw->write_ofs + mtdraw->write_fill != offset)
 		return -EINVAL;
-	if (mtdraw->write_fill == 0 && offset % bsz)
+	if (mtdraw->write_fill == 0 && offset - numblock * mtd->writesize != 0)
 		return -EINVAL;
 
 	if (mtdraw->write_fill) {
@@ -196,16 +210,16 @@ static ssize_t mtdraw_write(struct cdev *cdev, const void *buf, size_t count,
 	}
 
 	if (mtdraw->write_fill == bsz) {
-		numpage = mtdraw->write_ofs / (mtd->writesize + mtd->oobsize);
+		numblock = mtdraw_offset_to_block(mtd, mtdraw->write_ofs);
 		ret = mtdraw_blkwrite(mtd, mtdraw->writebuf,
-				      mtd->writesize * numpage);
+				      mtd->writesize * numblock);
 		mtdraw->write_fill = 0;
 	}
 
-	numpage = offset / (mtd->writesize + mtd->oobsize);
+	numblock = mtdraw_offset_to_block(mtd, offset);
 	while (ret >= 0 && count >= bsz) {
 		ret = mtdraw_blkwrite(mtd, buf + retlen,
-				   mtd->writesize * numpage++);
+				   mtd->writesize * numblock++);
 		count -= ret;
 		retlen += ret;
 		offset += ret;
@@ -225,15 +239,14 @@ static ssize_t mtdraw_write(struct cdev *cdev, const void *buf, size_t count,
 	}
 }
 
-static int mtdraw_erase(struct cdev *cdev, size_t count, loff_t _offset)
+static int mtdraw_erase(struct cdev *cdev, size_t count, loff_t offset)
 {
 	struct mtd_info *mtd = to_mtd(cdev);
 	struct erase_info erase;
-	unsigned long offset = _offset;
 	int ret;
 
-	offset = offset / (mtd->writesize + mtd->oobsize) * mtd->writesize;
-	count = count / (mtd->writesize + mtd->oobsize) * mtd->writesize;
+	offset = mtdraw_raw_to_mtd_offset(mtd, offset);
+	count = mtdraw_raw_to_mtd_offset(mtd, count);
 
 	memset(&erase, 0, sizeof(erase));
 	erase.mtd = mtd;
@@ -241,7 +254,7 @@ static int mtdraw_erase(struct cdev *cdev, size_t count, loff_t _offset)
 	erase.len = mtd->erasesize;
 
 	while (count > 0) {
-		debug("erase %d %d\n", erase.addr, erase.len);
+		debug("erase 0x%08llx len: 0x%08llx\n", erase.addr, erase.len);
 
 		if (!mtd->allow_erasebad)
 			ret = mtd_block_isbad(mtd, erase.addr);
@@ -249,7 +262,7 @@ static int mtdraw_erase(struct cdev *cdev, size_t count, loff_t _offset)
 			ret = 0;
 
 		if (ret > 0) {
-			printf("Skipping bad block at 0x%08x\n", erase.addr);
+			printf("Skipping bad block at 0x%08llx\n", erase.addr);
 		} else {
 			ret = mtd_erase(mtd, &erase);
 			if (ret)
-- 
2.7.0.rc3


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

  parent reply	other threads:[~2016-02-09  9:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-09  9:55 [PATCH] mtd: >4GiB fixes Sascha Hauer
2016-02-09  9:55 ` [PATCH 1/7] mtd: nand-bb: Fix 8k page size nands Sascha Hauer
2016-02-09  9:55 ` Sascha Hauer [this message]
2016-02-09  9:55 ` [PATCH 3/7] mtd: Make erase_info structs 64bit where necessary Sascha Hauer
2016-02-09  9:55 ` [PATCH 4/7] mtd: Fix mtd_op_read for devices >4GiB Sascha Hauer
2016-02-09  9:55 ` [PATCH 5/7] mtd: Fix mtd_op_erase " Sascha Hauer
2016-02-09  9:55 ` [PATCH 6/7] mtd: Fix erasing of " Sascha Hauer
2016-02-09  9:55 ` [PATCH 7/7] mtd: mtdoob device: change name to have the chip name first Sascha Hauer

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=1455011748-5538-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