mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
To: barebox@lists.infradead.org
Subject: [PATCH 1/1] mtd: nand: fix unaligned write support
Date: Wed,  5 Dec 2012 18:15:20 +0100	[thread overview]
Message-ID: <1354727720-23063-1-git-send-email-plagnioj@jcrosoft.com> (raw)
In-Reply-To: <20121205165337.GJ8327@game.jcrosoft.org>

via cdev we may write unaligned data the code was drop in the commit
0a4b1c7e440a81819eb2137f923573a3055dc7a2
mtd core: call driver write function with complete buffer

which is true for spi flashes but the code is still mandatory on nand

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 drivers/mtd/core.c            |    3 --
 drivers/mtd/nand/nand_write.c |   66 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 65 insertions(+), 4 deletions(-)

diff --git a/drivers/mtd/core.c b/drivers/mtd/core.c
index b5916da..8908f22 100644
--- a/drivers/mtd/core.c
+++ b/drivers/mtd/core.c
@@ -58,9 +58,6 @@ static 	ssize_t mtd_read(struct cdev *cdev, void* buf, size_t count,
 	return retlen;
 }
 
-#define NOTALIGNED(x) (x & (mtd->writesize - 1)) != 0
-#define MTDPGALG(x) ((x) & ~(mtd->writesize - 1))
-
 #ifdef CONFIG_MTD_WRITE
 static ssize_t mtd_write(struct cdev* cdev, const void *buf, size_t _count,
 			  loff_t _offset, ulong flags)
diff --git a/drivers/mtd/nand/nand_write.c b/drivers/mtd/nand/nand_write.c
index 5ed04ce..e49db43 100644
--- a/drivers/mtd/nand/nand_write.c
+++ b/drivers/mtd/nand/nand_write.c
@@ -363,7 +363,7 @@ int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
  *
  * NAND write with ECC
  */
-int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
+int __nand_write(struct mtd_info *mtd, loff_t to, size_t len,
 			  size_t *retlen, const uint8_t *buf)
 {
 	struct nand_chip *chip = mtd->priv;
@@ -386,6 +386,70 @@ int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
 	return ret;
 }
 
+static int all_ff(const void *buf, int len)
+{
+	int i;
+	const uint8_t *p = buf;
+
+	for (i = 0; i < len; i++)
+		if (p[i] != 0xFF)
+			return 0;
+	return 1;
+}
+
+#define MTD_NOTALIGNED(x) (x & (mtd->writesize - 1)) != 0
+#define MTDPGALG(x) ((x) & ~(mtd->writesize - 1))
+
+int nand_write(struct mtd_info *mtd, loff_t _offset, size_t _count,
+			  size_t *retlen, const uint8_t *buf)
+{
+	size_t now;
+	int ret = 0;
+	void *wrbuf = NULL;
+	size_t count = _count;
+	unsigned long offset = _offset;
+
+	if (MTD_NOTALIGNED(offset)) {
+		printf("offset 0x%0lx not page aligned\n", offset);
+		return -EINVAL;
+	}
+
+	dev_dbg(mtd->parent, "write: offset: 0x%08lx count: 0x%zx\n", offset, count);
+	while (count) {
+		now = count > mtd->writesize ? mtd->writesize : count;
+
+		if (MTD_NOTALIGNED(now)) {
+			dev_dbg(mtd->parent, "not aligned: %d %ld\n",
+				mtd->writesize,
+				(offset % mtd->writesize));
+			wrbuf = xmalloc(mtd->writesize);
+			memset(wrbuf, 0xff, mtd->writesize);
+			memcpy(wrbuf + (offset % mtd->writesize), buf, now);
+			if (!all_ff(wrbuf, mtd->writesize))
+				ret = __nand_write(mtd, MTDPGALG(offset),
+						  mtd->writesize, retlen,
+						  wrbuf);
+			free(wrbuf);
+		} else {
+			if (!all_ff(buf, mtd->writesize))
+				ret = __nand_write(mtd, offset, now, retlen,
+						  buf);
+			dev_dbg(mtd->parent,
+				"offset: 0x%08lx now: 0x%zx retlen: 0x%zx\n",
+				offset, now, *retlen);
+		}
+		if (ret)
+			goto out;
+
+		offset += now;
+		count -= now;
+		buf += now;
+	}
+
+out:
+	return ret ? ret : _count;
+}
+
 /**
  * nand_do_write_oob - [MTD Interface] NAND write out-of-band
  * @mtd:	MTD device structure
-- 
1.7.10.4


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

  reply	other threads:[~2012-12-05 17:17 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-29 19:03 [PATCH 1/2] mtd oob: do not register oob device for devices without oob Sascha Hauer
2012-11-29 19:03 ` [PATCH 2/2] mtd core: call driver write function with complete buffer Sascha Hauer
2012-12-03 19:10   ` Robert Jarzmik
2012-12-05 16:53   ` Jean-Christophe PLAGNIOL-VILLARD
2012-12-05 17:15     ` Jean-Christophe PLAGNIOL-VILLARD [this message]
2012-12-05 18:28       ` [PATCH 1/1] mtd: nand: fix unaligned write support Sascha Hauer
2012-12-07 13:17         ` Jean-Christophe PLAGNIOL-VILLARD

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=1354727720-23063-1-git-send-email-plagnioj@jcrosoft.com \
    --to=plagnioj@jcrosoft.com \
    --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