Skip to contentSkip to conversation

Add write-while-program operation

RFC PATCH In review Adrian Hunter 2 participants 5 messages Aug 14, 2008 – Oct 15, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Add write-while-program operation

---

drivers/mtd/onenand/omap2.c        |    2 +
drivers/mtd/onenand/onenand_base.c |  104 ++++++++++++++++++++++++++++++++++++
include/linux/mtd/onenand.h        |    4 ++
3 files changed, 110 insertions(+), 0 deletions(-)
drivers/mtd/onenand/omap2.c +0 -0
index bf6bba5..236a68c 100644
@@ -674,6 +674,8 @@ static int __devinit omap2_onenand_probe(struct platform_device *pdev)
	if ((r = onenand_scan(&c->mtd, 1)) < 0)
		goto err_release_dma;

+ c->mtd.write = onenand_write_while_prog;
+

	switch ((c->onenand.version_id >> 4) & 0xf) {
	case 0:
		c->freq = 40;
drivers/mtd/onenand/onenand_base.c +0 -0
index 5d7965f..ce10362 100644
@@ -1573,6 +1573,110 @@ static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
	return ret;
}

+/**
+ * onenand_write_while_prog - [MTD Interface] write while program
+ * @param mtd MTD device structure
+ * @param to offset to write to
+ * @param len number of bytes to write
+ * @param retlen pointer to variable to store the number of written bytes
+ * @param buf the data to write
+ *
+ * Write while program.
+ */
+int onenand_write_while_prog(struct mtd_info *mtd, loff_t to, size_t len,
+ size_t *retlen, const u_char *buf)
+{
+ struct onenand_chip *this = mtd->priv;
+ int written = 0, column, uninitialized_var(thislen), uninitialized_var(lastlen);
+ int ret = 0, first_time = 1, uninitialized_var(subpage), uninitialized_var(prev_subpage);
+ loff_t prev = to;
+
+ onenand_get_device(mtd, FL_WRITING);
+
+ DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_while_prog: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
+
+ /* Initialize retlen, in case of early exit */
+ *retlen = 0;
+
+ /* Do not allow writes past end of device */
+ if (unlikely((to + len) > mtd->size)) {
+ printk(KERN_ERR "onenand_write_while_prog: Attempt write to past end of device\n");
+ onenand_release_device(mtd);
+ return -EINVAL;
+ }
+
+ /* Reject writes, which are not page aligned */
+ if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
+ printk(KERN_ERR "onenand_write_while_prog: Attempt to write not page aligned data\n");
+ onenand_release_device(mtd);
+ return -EINVAL;
+ }
+
+ if (!len) {
+ onenand_release_device(mtd);
+ return 0;
+ }
+
+ column = to & (mtd->writesize - 1);
+
+ /* Loop until all data is written */
+ while (1) {
+ if (written < len) {
+ u_char *wbuf = (u_char *)buf;
+
+ thislen = min_t(int, mtd->writesize - column, len - written);
+
+ cond_resched();
+
+ this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
+
+ subpage = thislen < mtd->writesize;
+ if (subpage) {
+ memset(this->page_buf, 0xff, mtd->writesize);
+ memcpy(this->page_buf + column, buf, thislen);
+ wbuf = this->page_buf;
+ }
+
+ this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
+ this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
+ } else
+ ONENAND_SET_NEXT_BUFFERRAM(this);
+
+ if (!first_time) {
+ ONENAND_SET_PREV_BUFFERRAM(this);
+ ret = this->wait(mtd, FL_WRITING);
+ onenand_update_bufferram(mtd, prev, !ret && !prev_subpage);
+ if (ret) {
+ written -= lastlen;
+ printk(KERN_ERR "onenand_write_while_prog: write failed %d\n", ret);
+ break;
+ }
+ if (written == len) {
+ ret = onenand_verify(mtd, buf - len, to - len, len);
+ if (ret)
+ printk(KERN_ERR "onenand_write_while_prog: verify failed %d\n", ret);
+ break;
+ }
+ ONENAND_SET_NEXT_BUFFERRAM(this);
+ }
+
+ this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
+
+ written += thislen;
+ column = 0;
+ prev_subpage = subpage;
+ prev = to;
+ to += thislen;
+ buf += thislen;
+ lastlen = thislen;
+ first_time = 0;
+ }
+
+ *retlen = written;
+
+ onenand_release_device(mtd);
+ return ret;
+}

/**
* onenand_write_oob_nolock - [Internal] OneNAND write out-of-band

include/linux/mtd/onenand.h +0 -0
index 9aa2a91..1223430 100644
@@ -24,6 +24,10 @@ extern int onenand_scan(struct mtd_info *mtd, int max_chips);

/* Free resources held by the OneNAND device */ extern void onenand_release(struct mtd_info *mtd);

+/* Alternative write algorithm : write-while program */
+int onenand_write_while_prog(struct mtd_info *mtd, loff_t to, size_t len,
+ size_t *retlen, const u_char *buf);
+
/*
* onenand_state_t - chip states
* Enumeration for OneNAND flash chip state

Signature · 1 line
1.5.4.3
#1 lore raw

Hi Adrian,

Before comment the patch, I first wonder which mtd user can use this feature?

To work correctly, the upper layer sends more than 2 pages write size. umm just override the write buffer in omap2. maybe assume mtdblock?

Thank you,
Kyungmin Park

Quoted 175 lines · Adrian Hunter

---
drivers/mtd/onenand/omap2.c | 2 +
drivers/mtd/onenand/onenand_base.c | 104
++++++++++++++++++++++++++++++++++++
include/linux/mtd/onenand.h | 4 ++

3 files changed, 110 insertions(+), 0 deletions(-)
drivers/mtd/onenand/omap2.c +2 -0
index bf6bba5..236a68c 100644
@@ -674,6 +674,8 @@ static int __devinit omap2_onenand_probe(struct
 platform_device *pdev)
       if ((r = onenand_scan(&c->mtd, 1)) < 0)
               goto err_release_dma;
 
+       c->mtd.write = onenand_write_while_prog;
+
       switch ((c->onenand.version_id >> 4) & 0xf) {
       case 0:
               c->freq = 40;
drivers/mtd/onenand/onenand_base.c +6 -0
index 5d7965f..ce10362 100644
@@ -1573,6 +1573,110 @@ static int onenand_write_ops_nolock(struct mtd_info
 *mtd, loff_t to,
       return ret;
 }
 
+/**
+ * onenand_write_while_prog - [MTD Interface] write while program
+ * @param mtd          MTD device structure
+ * @param to           offset to write to
+ * @param len          number of bytes to write
+ * @param retlen       pointer to variable to store the number of written

bytes
+ * @param buf the data to write
+ *
+ * Write while program.
+ */

drivers/mtd/onenand/onenand_base.c +5 -0
+int onenand_write_while_prog(struct mtd_info *mtd, loff_t to, size_t len,
+                            size_t *retlen, const u_char *buf)
+{
+       struct onenand_chip *this = mtd->priv;
+       int written = 0, column, uninitialized_var(thislen),

uninitialized_var(lastlen);
+ int ret = 0, first_time = 1, uninitialized_var(subpage),
uninitialized_var(prev_subpage);

drivers/mtd/onenand/onenand_base.c +5 -0
+       loff_t prev = to;
+
+       onenand_get_device(mtd, FL_WRITING);
+
+       DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_while_prog: to = 0x%08x, len

= %i\n", (unsigned int) to, (int) len);

drivers/mtd/onenand/onenand_base.c +7 -0
+
+       /* Initialize retlen, in case of early exit */
+       *retlen = 0;
+
+       /* Do not allow writes past end of device */
+       if (unlikely((to + len) > mtd->size)) {
+               printk(KERN_ERR "onenand_write_while_prog: Attempt write to

past end of device\n");

drivers/mtd/onenand/onenand_base.c +7 -0
+               onenand_release_device(mtd);
+               return -EINVAL;
+       }
+
+       /* Reject writes, which are not page aligned */
+        if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
+                printk(KERN_ERR "onenand_write_while_prog: Attempt to write

not page aligned data\n");

drivers/mtd/onenand/onenand_base.c +17 -0
+               onenand_release_device(mtd);
+                return -EINVAL;
+        }
+
+       if (!len) {
+               onenand_release_device(mtd);
+               return 0;
+       }
+
+       column = to & (mtd->writesize - 1);
+
+       /* Loop until all data is written */
+       while (1) {
+               if (written < len) {
+                       u_char *wbuf = (u_char *)buf;
+
+                       thislen = min_t(int, mtd->writesize - column, len -

written);

drivers/mtd/onenand/onenand_base.c +4 -0
+
+                       cond_resched();
+
+                       this->command(mtd, ONENAND_CMD_BUFFERRAM, to,

thislen);

drivers/mtd/onenand/onenand_base.c +4 -0
+
+                       subpage = thislen < mtd->writesize;
+                       if (subpage) {
+                               memset(this->page_buf, 0xff,

mtd->writesize);
+ memcpy(this->page_buf + column, buf,
thislen);

drivers/mtd/onenand/onenand_base.c +4 -0
+                               wbuf = this->page_buf;
+                       }
+
+                       this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0,

mtd->writesize);
+ this->write_bufferram(mtd, ONENAND_SPARERAM,
ffchars, 0, mtd->oobsize);

drivers/mtd/onenand/onenand_base.c +7 -0
+               } else
+                       ONENAND_SET_NEXT_BUFFERRAM(this);
+
+               if (!first_time) {
+                       ONENAND_SET_PREV_BUFFERRAM(this);
+                       ret = this->wait(mtd, FL_WRITING);
+                       onenand_update_bufferram(mtd, prev, !ret &&

!prev_subpage);

drivers/mtd/onenand/onenand_base.c +3 -0
+                       if (ret) {
+                               written -= lastlen;
+                               printk(KERN_ERR "onenand_write_while_prog:

write failed %d\n", ret);

drivers/mtd/onenand/onenand_base.c +4 -0
+                               break;
+                       }
+                       if (written == len) {
+                               ret = onenand_verify(mtd, buf - len, to -

len, len);

drivers/mtd/onenand/onenand_base.c +2 -0
+                               if (ret)
+                                       printk(KERN_ERR

"onenand_write_while_prog: verify failed %d\n", ret);

drivers/mtd/onenand/onenand_base.c +22 -0
+                               break;
+                       }
+                       ONENAND_SET_NEXT_BUFFERRAM(this);
+               }
+
+               this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
+
+               written += thislen;
+               column = 0;
+               prev_subpage = subpage;
+               prev = to;
+               to += thislen;
+               buf += thislen;
+               lastlen = thislen;
+               first_time = 0;
+       }
+
+       *retlen = written;
+
+       onenand_release_device(mtd);
+       return ret;
+}

/**
* onenand_write_oob_nolock - [Internal] OneNAND write out-of-band

include/linux/mtd/onenand.h +4 -0
index 9aa2a91..1223430 100644
@@ -24,6 +24,10 @@ extern int onenand_scan(struct mtd_info *mtd, int
 max_chips);
 /* Free resources held by the OneNAND device */
 extern void onenand_release(struct mtd_info *mtd);
 
+/* Alternative write algorithm : write-while program */
+int onenand_write_while_prog(struct mtd_info *mtd, loff_t to, size_t len,
+                            size_t *retlen, const u_char *buf);
+
 /*
 * onenand_state_t - chip states

* Enumeration for OneNAND flash chip state
--
1.5.4.3

#2 lore raw
Quoted 1 line · Kyungmin Park

Before comment the patch, I first wonder which mtd user can use this feature?

Well, UBI does large writes when it moves PEBs (for wear leveling or in response to bit-flips), or for atomic LEB change, or when it updates volumes.

Quoted 2 lines

To work correctly, the upper layer sends more than 2 pages write size. umm just override the write buffer in omap2. maybe assume mtdblock?

I am not sure what you mean here.

#3 lore raw
In reply to Kyungmin Park
Quoted 9 lines · Kyungmin Park

Hi Adrian,

Before comment the patch, I first wonder which mtd user can use this feature?

To work correctly, the upper layer sends more than 2 pages write size. umm just override the write buffer in omap2. maybe assume mtdblock?

Thank you,
Kyungmin Park

We have decided that we would like to use write-while-program. Do you have any objections to including this in the OneNAND driver?

#4 lore raw

Hi Adrian,

Quoted 4 lines

We have decided that we would like to use write-while-program. Do you have any objections to including this in the OneNAND driver?

Could you merge this one to ops style?
I think no problem to merge existing write_ops_operation instead of new function.

Thank you,
Kyungmin Park