Skip to contentSkip to conversation

mtd: nand: Add driver for M-sys / Sandisk diskonchip G4

PATCH Superseded Mike Dunn 3 participants 10 messages Nov 2, 2011 – Nov 4, 2011

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Mike Dunn

author
#0 lore raw

PATCH v2 1/1 mtd: nand: Add driver for M-sys / Sandisk diskonchip G4

This is a nand driver for the diskonchip G4 in my Palm Treo680. It's been fairly well tested; it passes the nandtest utility in mtd-utils, and also the kernel tests mtd_pagetest and mtd_readtest. A ubifs was created on it and seems to be working well, though stress testing is needed.

This version 2 includes a lot of cleanup, stylistic improvements, removal of BCH algorithmic code that was redundant with the existing kernel algorithm, and some refactoring. The #defines related to the device registers were made consistent with those in the diskonchip G3 driver currently under development, in anticipation of a shared header file (at least) between the two drivers at some point. Thanks to everyone who took the time to review and comment on the earlier version.

Hopefully some mtd experts can share their opinion on the suitability of the nand interface for these devices. This G4 driver uses it, the G3 driver does not. It would probably be a bad thing for two similiar devices to interface with the mtd subsystem differently.

---

This was tested against the latest mtd-2.6 kernel from the git repository on infradead.org, but I also verified that it builds against Artem's l2-mtd-2.6 kernel.

 drivers/mtd/nand/Kconfig  |   12 +
 drivers/mtd/nand/Makefile |    1 +
 drivers/mtd/nand/docg4.c  | 1199 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/mtd/docg4.h |   24 +
 4 files changed, 1236 insertions(+), 0 deletions(-)
 create mode 100644 drivers/mtd/nand/docg4.c
 create mode 100644 include/linux/mtd/docg4.h
drivers/mtd/nand/Kconfig +12 -0
index 4c34252..abb5a33 100644
@@ -319,6 +319,18 @@ config MTD_NAND_DISKONCHIP_BBTWRITE
 	  load time (assuming you build diskonchip as a module) with the module
 	  parameter "inftl_bbt_write=1".
 
+config MTD_NAND_DOCG4
+	tristate "Support for DiskOnChip G4 (EXPERIMENTAL)"
+	depends on EXPERIMENTAL
+	select BCH
+	select BITREVERSE
+	help
+	  Support for diskonchip G4 nand flash, found in several smartphones,
+	  such as the Palm Treo680 and HTC Prophet.
+
+	  There is currently no support for the saftl filesystem; however a
+	  ubifs has been succesfully tested on the device.
+
 config MTD_NAND_SHARPSL
 	tristate "Support for NAND Flash on Sharp SL Series (C7xx + others)"
 	depends on ARCH_PXA
drivers/mtd/nand/Makefile +1 -0
index 5745d83..70993e7 100644
@@ -20,6 +20,7 @@ obj-$(CONFIG_MTD_NAND_PPCHAMELEONEVB)	+= ppchameleonevb.o
 obj-$(CONFIG_MTD_NAND_S3C2410)		+= s3c2410.o
 obj-$(CONFIG_MTD_NAND_DAVINCI)		+= davinci_nand.o
 obj-$(CONFIG_MTD_NAND_DISKONCHIP)	+= diskonchip.o
+obj-$(CONFIG_MTD_NAND_DOCG4)		+= docg4.o
 obj-$(CONFIG_MTD_NAND_FSMC)		+= fsmc_nand.o
 obj-$(CONFIG_MTD_NAND_H1900)		+= h1910.o
 obj-$(CONFIG_MTD_NAND_RTC_FROM4)	+= rtc_from4.o
drivers/mtd/nand/docg4.c new file +1199 -0

1,199 lines not on this page · raw message

include/linux/mtd/docg4.h new file +24 -0
new file mode 100644
index 0000000..654699c
@@ -0,0 +1,24 @@
+/*
+ *  Copyright (C) 2011 Mike Dunn <[email protected]>
+ *
+ * Nand mtd driver for M-Systems DiskOnChip G4 device
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+struct docg4_nand_platform_data {
+	struct mtd_partition *partitions;
+	unsigned int nr_partitions;
+};
Signature · 1 line
1.7.3.4
#1 lore raw

On Wed, Nov 02, 2011 at 11:00:59PM +0000, Mike Dunn wrote:

Hi Mike,
A few comments below:

drivers/mtd/nand/docg4.c line 188

Quoted 4 lines
drivers/mtd/nand/docg4.c new file +4 -0
+
+/* value generated by the HW ecc generator upon reading blank page */
+static uint8_t blank_read_hwecc[7] = {
+       0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9 };

Using this blank ecc value to detect blank pages after reading them is not that reliable, because if a bitflip appears in an erased page, the HW ecc generator will generate a completely different sequence of bytes. Your driver will think the page is not blank, it will try to correct errors and fail. And UBIFS will not appreciate that.

I can see two cleaner alternatives to solve this issue:

1. When you program a page, before writing hwecc to oob, adjust it like this:

   hwecc[i] ^= blank_read_hwecc[i]^0xff;

The effect of this is that you now get 0xffs as ecc for blank pages, and bitflip correction on erased pages for free. This is transparent to your controller, because this "adjustment" cancels itself upon reading when calc_ecc^recv_ecc is computed.

2. Use unprotected spare oob byte 15 as a programmming marker: remove it from the oob_free list, and force it to 0 when you program a page. Now, you can easily detect if a page is blank or has been programmed by checking this byte. You can for instance count the number of bits set to 1 in the byte, and decide it is blank if that number is greater than 4; this ensures you are robust to bitflips in the marker byte itself.

My preference would go to option 2 in your case.

drivers/mtd/nand/docg4.c line 377

Quoted 9 lines
drivers/mtd/nand/docg4.c new file +9 -0
+
+       /* fix the errors */
+       fixederrs = 0;
+       for (i = 0; i < numerrs; i++) {
+               if (errpos[i] > DOCG4_USERDATA_LEN * 8)
+                       continue; /* error in ecc oob bytes; ignore */
+               change_bit(errpos[i], (unsigned long *)buf);
+               fixederrs++;
+       }

Here, your check on errorpos[i] to skip ecc oob locations is correct; but a bitflip (in ecc bytes or elsewhere) is an indication that the nand page needs scrubbing (you don't want bitflips to accumulate), so you should increase fixederrs in that case also.

If you see a _lot_ of scrubbing operations from UBI, you may also consider concealing bitflip corrections; i.e. report them only if the number of corrected bits is above a certain threshold (e.g. >= 3 in your case).

BR,
--
Ivan

#2 lore raw

Hi,

Quoted 27 lines · Ivan Djelic

On Wed, Nov 02, 2011 at 11:00:59PM +0000, Mike Dunn wrote:

Hi Mike,
A few comments below:

drivers/mtd/nand/docg4.c line 188

Quoted 4 lines
drivers/mtd/nand/docg4.c new file +4 -0
+
+/* value generated by the HW ecc generator upon reading blank page */
+static uint8_t blank_read_hwecc[7] = {
+       0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9 };

Using this blank ecc value to detect blank pages after reading them is not that reliable, because if a bitflip appears in an erased page, the HW ecc generator will generate a completely different sequence of bytes. Your driver will think the page is not blank, it will try to correct errors and fail. And UBIFS will not appreciate that.

I can see two cleaner alternatives to solve this issue:

1. When you program a page, before writing hwecc to oob, adjust it like this:

   hwecc[i] ^= blank_read_hwecc[i]^0xff;

The effect of this is that you now get 0xffs as ecc for blank pages, and bitflip correction on erased pages for free. This is transparent to your controller, because this "adjustment" cancels itself upon reading when calc_ecc^recv_ecc is computed.

Quoted 9 lines

2. Use unprotected spare oob byte 15 as a programmming marker: remove it from the oob_free list, and force it to 0 when you program a page. Now, you can easily detect if a page is blank or has been programmed by checking this byte. You can for instance count the number of bits set to 1 in the byte, and decide it is blank if that number is greater than 4; this ensures you are robust to bitflips in the marker byte itself.

My preference would go to option 2 in your case.

Note that UBIFS except blank page to be 0xff.

With option 1 you have nothing to do (ecc correct bit-flips), with option 2 you have to memset the page (data+spare) to 0xff to clear bit-flips.

Also with option 2 you don't know how many bit-flip there are in the blank page. Because UBIFS (or mtd) don't check the page after a write , you can end writting a page with too many bit-flips without any error.

Matthieu

author
#3 lore raw
Quoted 28 lines · Matthieu CASTET
Quoted 18 lines · Ivan Djelic

Using this blank ecc value to detect blank pages after reading them is not that reliable, because if a bitflip appears in an erased page, the HW ecc generator will generate a completely different sequence of bytes. Your driver will think the page is not blank, it will try to correct errors and fail. And UBIFS will not appreciate that.

I can see two cleaner alternatives to solve this issue:

1. When you program a page, before writing hwecc to oob, adjust it like this:

   hwecc[i] ^= blank_read_hwecc[i]^0xff;

The effect of this is that you now get 0xffs as ecc for blank pages, and bitflip correction on erased pages for free. This is transparent to your controller, because this "adjustment" cancels itself upon reading when calc_ecc^recv_ecc is computed.

Quoted 9 lines

2. Use unprotected spare oob byte 15 as a programmming marker: remove it from the oob_free list, and force it to 0 when you program a page. Now, you can easily detect if a page is blank or has been programmed by checking this byte. You can for instance count the number of bits set to 1 in the byte, and decide it is blank if that number is greater than 4; this ensures you are robust to bitflips in the marker byte itself.

My preference would go to option 2 in your case.

Quoted 10 lines

Note that UBIFS except blank page to be 0xff.

With option 1 you have nothing to do (ecc correct bit-flips), with option 2 you have to memset the page (data+spare) to 0xff to clear bit-flips.

Also with option 2 you don't know how many bit-flip there are in the blank page. Because UBIFS (or mtd) don't check the page after a write , you can end writting a page with too many bit-flips without any error.

How about implementing both? The oob byte 15 can be used to check if the page is blank only in the event that bch decode fails due to the number of bit errors exceeding four.

I really appreciate the advice, gentlemen!

Thanks,
Mike

author
#4 lore raw
In reply to Matthieu CASTET
Quoted 12 lines · Matthieu CASTET

Hi,

Quoted 9 lines · Ivan Djelic

1. When you program a page, before writing hwecc to oob, adjust it like this:

   hwecc[i] ^= blank_read_hwecc[i]^0xff;

The effect of this is that you now get 0xffs as ecc for blank pages, and bitflip correction on erased pages for free. This is transparent to your controller, because this "adjustment" cancels itself upon reading when calc_ecc^recv_ecc is computed.

Not entirely transparent. I didn't think this through. Modifying the hw-generated ecc bytes causes the hw to think that there were error(s) on every page. True, the driver will know better, but it will slow things down because ordinarily the ecc unit only has to be read when the hardware reports errors. With this scheme, an error is reported and the ecc bytes read and processed on every page.

Quoted 9 lines
Quoted 9 lines

2. Use unprotected spare oob byte 15 as a programmming marker: remove it from the oob_free list, and force it to 0 when you program a page. Now, you can easily detect if a page is blank or has been programmed by checking this byte. You can for instance count the number of bits set to 1 in the byte, and decide it is blank if that number is greater than 4; this ensures you are robust to bitflips in the marker byte itself.

My preference would go to option 2 in your case.

I'm going to implement option 2 for now...

Quoted 8 lines

Note that UBIFS except blank page to be 0xff.

With option 1 you have nothing to do (ecc correct bit-flips), with option 2 you have to memset the page (data+spare) to 0xff to clear bit-flips.

Also with option 2 you don't know how many bit-flip there are in the blank page. Because UBIFS (or mtd) don't check the page after a write , you can end writting a page with too many bit-flips without any error.

...keeping these things in mind during testing. A warning is printedto the kernel log when a bank page is read with errors. If I see this occurring, maybe the performance hit of option 1 is warranted.

Thanks again gentlemen.

Mike

#5 lore raw
Quoted 15 lines · Mike Dunn
Quoted 12 lines · Matthieu CASTET

Hi,

Quoted 9 lines · Ivan Djelic

1. When you program a page, before writing hwecc to oob, adjust it like this:

   hwecc[i] ^= blank_read_hwecc[i]^0xff;

The effect of this is that you now get 0xffs as ecc for blank pages, and bitflip correction on erased pages for free. This is transparent to your controller, because this "adjustment" cancels itself upon reading when calc_ecc^recv_ecc is computed.

Quoted 6 lines

Not entirely transparent. I didn't think this through. Modifying the hw-generated ecc bytes causes the hw to think that there were error(s) on every page. True, the driver will know better, but it will slow things down because ordinarily the ecc unit only has to be read when the hardware reports errors. With this scheme, an error is reported and the ecc bytes read and processed on every page.

So you did check this with a real-life test ?
My assumption was that the HW relied entirely on recv_ecc^calc_ecc to decide that an error had occured. This recv_ecc^calc_ecc polynomial is the _same_ whether you apply the suggested XOR masking or not (that's the trick).

If your HW reports an error on every page when you apply the XOR masking, it means it does itself some sort of check (which has probably nothing to do with BCH decoding) on oob ecc bytes; this would be consistent with the HW flag Robert mentioned (the one that seems to indicate that a page has been programmed).

BR,
--
Ivan

#6 lore raw
Quoted 16 lines · Ivan Djelic
Quoted 15 lines · Mike Dunn
Quoted 12 lines · Matthieu CASTET

Hi,

Quoted 9 lines · Ivan Djelic

1. When you program a page, before writing hwecc to oob, adjust it like this:

   hwecc[i] ^= blank_read_hwecc[i]^0xff;

The effect of this is that you now get 0xffs as ecc for blank pages, and bitflip correction on erased pages for free. This is transparent to your controller, because this "adjustment" cancels itself upon reading when calc_ecc^recv_ecc is computed.

Quoted 7 lines
Quoted 6 lines

Not entirely transparent. I didn't think this through. Modifying the hw-generated ecc bytes causes the hw to think that there were error(s) on every page. True, the driver will know better, but it will slow things down because ordinarily the ecc unit only has to be read when the hardware reports errors. With this scheme, an error is reported and the ecc bytes read and processed on every page.

Oops, forget what I said in my previous post; I should have said the opposite:
that is, my XOR masking idea only works if the controller does not compute recv_ecc^calc_ecc itself. So indeed you're right, it's not useful in your case.

BR,
--
Ivan

author
#7 lore raw
Quoted 9 lines · Ivan Djelic
Quoted 8 lines · Ivan Djelic
Quoted 7 lines · Mike Dunn

Not entirely transparent. I didn't think this through. Modifying the hw-generated ecc bytes causes the hw to think that there were error(s) on every page. True, the driver will know better, but it will slow things down because ordinarily the ecc unit only has to be read when the hardware reports errors. With this scheme, an error is reported and the ecc bytes read and processed on every page.

Quoted 3 lines

Oops, forget what I said in my previous post; I should have said the opposite:
that is, my XOR masking idea only works if the controller does not compute recv_ecc^calc_ecc itself. So indeed you're right, it's not useful in your case.

OK makes sense again. I was getting confused (though that's easy for me to do). Yeah, no error if hw calculates recv_ecc^calc_ecc == 0. Should still work, though, if I'm willing to take the performance hit of processing the "error" and reading out hw ecc on every read.

Thanks again for the tips. Matthieu also.

Mike

author
#8 lore raw
In reply to Ivan Djelic
Quoted 15 lines · Ivan Djelic

Hi Mike,
A few comments below:

drivers/mtd/nand/docg4.c line 188

Quoted 4 lines
drivers/mtd/nand/docg4.c new file +4 -0
+
+/* value generated by the HW ecc generator upon reading blank page */
+static uint8_t blank_read_hwecc[7] = {
+       0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9 };

Using this blank ecc value to detect blank pages after reading them is not that reliable, because if a bitflip appears in an erased page, the HW ecc generator will generate a completely different sequence of bytes. Your driver will think the page is not blank, it will try to correct errors and fail. And UBIFS will not appreciate that.

I can see two cleaner alternatives to solve this issue:

Quoted 8 lines

1. When you program a page, before writing hwecc to oob, adjust it like this:

   hwecc[i] ^= blank_read_hwecc[i]^0xff;

The effect of this is that you now get 0xffs as ecc for blank pages, and bitflip correction on erased pages for free. This is transparent to your controller, because this "adjustment" cancels itself upon reading when calc_ecc^recv_ecc is computed.

This is neat! Will try this today.

Quoted 8 lines

2. Use unprotected spare oob byte 15 as a programmming marker: remove it from the oob_free list, and force it to 0 when you program a page. Now, you can easily detect if a page is blank or has been programmed by checking this byte. You can for instance count the number of bits set to 1 in the byte, and decide it is blank if that number is greater than 4; this ensures you are robust to bitflips in the marker byte itself.

My preference would go to option 2 in your case.

Hmm, another good idea. I haven't given much thought to robustness until now. Heck, I never expected to get this driver working right at all. Once again, I'm in your debt, Ivan.

Quoted 9 lines

drivers/mtd/nand/docg4.c line 377

Quoted 9 lines
drivers/mtd/nand/docg4.c new file +9 -0
+
+       /* fix the errors */
+       fixederrs = 0;
+       for (i = 0; i < numerrs; i++) {
+               if (errpos[i] > DOCG4_USERDATA_LEN * 8)
+                       continue; /* error in ecc oob bytes; ignore */
+               change_bit(errpos[i], (unsigned long *)buf);
+               fixederrs++;
+       }
Quoted 4 lines

Here, your check on errorpos[i] to skip ecc oob locations is correct; but a bitflip (in ecc bytes or elsewhere) is an indication that the nand page needs scrubbing (you don't want bitflips to accumulate), so you should increase fixederrs in that case also.

Thanks. Will make this change.

Quoted 3 lines

If you see a _lot_ of scrubbing operations from UBI, you may also consider concealing bitflip corrections; i.e. report them only if the number of corrected bits is above a certain threshold (e.g. >= 3 in your case).

Something to keep in mind when I start stress testing with ubifs. I have noticed some scrubbing in the testing so far. At one point, it marked a block as bad, so this may be an issue. The error rate seems erratic. Sometimes there are many, and even the occasional uncorrectable error. Lately, there have just a few one-bit errors when I run nandtest over the whole device. I noticed this inconsistency while observing operation under the PalmOS code as well, so it's not a problem in my driver. Maybe due to solar activity :-)

Thanks again,
Mike

author
#9 lore raw
In reply to Mike Dunn
Quoted 5 lines · Mike Dunn

This was tested against the latest mtd-2.6 kernel from the git repository on infradead.org, but I also verified that it builds against Artem's l2-mtd-2.6 kernel.

FYI... this is incorrect. Sorry, I goofed. It will not build against the l2-mtd-2.6 kernel, due to use of DEBUG macros in mtd.h. I created the patch against the wrong file. Otherwise, the patch is good. Next version will not contain the DEBUG macros and will build against both kernels.

Thanks,
Mike